How to? Input number field to always show to 2 decimal places

I have lots of number input fields and everytime a number is manually typed in or a number comes from a query, I always want to show 2 decimal places. It is OK if someone types 5.25, but if they just type 5, I want the .00 added to the number. How can I do this?

image

image

image

Maybe use a Pattern?

@UKRiggers, this might help you out. How to Round a number with decimals with Server Data Formatter

You can use the same concept on the App Structure side as well.

Hi @scott, thanks for that, but it doesnā€™t help with a blank input field where the user types in a number ā€˜5ā€™ for example. It will show as 5 and not 5.00 or am I missing something?

Maybe use a Pattern?

Iā€™m not in a position to bring Wappler up, but I think it would apply because it would change the number from 5 to 5.00 upon submission.

You could also do a validate on the App Structure side to force the user to input the number in decimal format.

Cheers for that, Iā€™m trying to keep the number of keystrokes (for the user) to a minimum. And yes, I understand we can manipulate the data on submission to format to 2 decimal places but I want the user to see this as he enters the data. Iā€™ve done it before but just canā€™t remember how.

The blanks in my brain only used to happen on a Friday, now it happens everyday! :slightly_frowning_face:

I agree completely !

This may be of assistance https://plentz.github.io/jquery-maskmoney/

1 Like

Would this do it:
dmx-on:blur="text1.setValue(value.toNumber().formatNumber(2, ".", ","))"
?

5 Likes

or this thread

@Tom thatā€™s just what I wanted, thanks. I did try something very similar using key up but it didnā€™t work. I recreated your example and just like magic, it does what it says on the tin :smiley:

Great - Iā€™m glad that was useful. However, there is some small print on the tin: if you enter ā€˜12,000.00ā€™ or ā€˜12,000ā€™ for example and tab out of the field, ā€˜Invalid Numberā€™ will appear - and your number will be deleted. Itā€™s best that users donā€™t enter commas and trailing zeros etc. - and this will probably persuade them not to.

Thanks for the heads-up. I have had a play to see what happens. If the Input field is set to Number then I can only enter 3 (or 4) digits. If it is set as a Text field then there are only problems if you type in the comma separator. This will be OK with this particular client. I know how their previous system works.

I donā€™t think itā€™s a problem. Itā€™s good to have the extra validation included, though perhaps ideally it wouldnā€™t remove the invalid number in the process. Not much of an issue unless youā€™re entering very long numbers - in which case you could reinstate the invalid number to edit, by storing it in a variable and using that to repopulate the input field.

There are also problems if you type other invalid characters such as spaces or letters (but still, not much of an issue).

1 Like