UI formatting with Thymeleaf and Spring MVC - spring

I need to format values in the UI in different ways:
No decimals: 15
One decimal: 15.4
Two decimals: 15.44
Percentage no decimals: 15%
Percentage one decimal: 15.4%
Percentage two decimals: 15:44%
What is the recommended way to achieve this? To use Thymeleaf numbers formatter to format to integer or decimal (and add % sign myself if needed). Or should I configure one or more Spring formatters with different formatting patterns?

If you only show the value at one page I would format it in the template. If it's a form field or you need it on different pages I would use the converter/formatter-approach of spring.

Related

Only allow whole numbers but display with '.' on the thousand for easy read

I want to apply data validation to my column so as to only accept whole numbers.
However I want these to be displayed with a dot so as to make it easier to read later on.
e.g. input = 14354 which is valid and then displayed 14.354
the data validation regular expression I am ussing is:
=regexmatch(to_text(A2);"^\d+\.*\d+$")
and the custom formatting is:
#,##
for most this working fine, large numbers are displayed with the '.' and things it shouldnt accept it is rejecting.
However, in the case of numbers which are entered with a decimal point as these are hidden, it is accepting it as valid.
It is also changing the format to auto atic and reading as date such entries like: 15.4
I should point out that I am using sheets in spanish and therefore the , is the marker for decimal places.
What am i missing here??
Select the cell range then go to Data > Data validation...
Add a custom formula rule:
=mod(A1;1)=0
Try this one:
=and(regexmatch(to_text(A2);"^\d+(\.\d{3})*$");mod(A2;1)=0)
Improved your formula to only accept a dot when it is followed by 3 numbers (this way, we invalidate the date e.g A2)
Combining the improved formula of yours and Aresvik's modulo answer, we need to check if the value does not have decimal. (this way, we invalidate the decimal e.g A6)
When both returns true, this shall confirm that the number inputted is a whole number with no decimal and not a date.
Output:
Invalid inputted values:
A2 - 15.4
A6 - 16412,212

Format currency with dot instead of comma using i18n

We are using java.text.NumberFormat class to format the currency values using the method getInstance(Locale paramLocale). Our issue is when we pass es_CO(Columbia) language code it automatically formats it in value 123,00 instead of 123.00. Is there a way to format with dot instead of comma?
I am using Spring platform(hybris)
Please note due to business reasons it is not possible for me to change the locale.
You can use DecimalFormat to have your own format.
Look at this How can I format a String number to have commas and round?

How to avoid exception by mapping string to number in thymeleaf?

I have some HTML page and on this page I will provide the possibility for free text.
For example, it is possible to write in textbox either: 10 or 10 apples.
In a case of writing 10 apples I got NumberFormatException which is correct, but for me will be good to extract only number automatically without javascript writing.
Is it possible to map string from HTML page to the number in my java entity? May be with some annotation or somehow else?
Try:
final String stripped = textbox.getText().replaceAll("[^0-9]", "");
This takes the contents of the text field and strips out any characters that aren't digits. If you need to deal with floating point or negative numbers, it can be done, but becomes more complicated.

Spring localization truncates decimals

I'm using Spring localization with a properties file and basically want to localize {0}€ with the parameter being a BigDecimal. The localzied output will show 1€ instead of 1.00€ while 1.01€ will be displayed as should be.
Is there a way to leave blank decimal places untouched?
You can define message format for numbers this way
product.price = Price: {0, number, #.##}€
See Dealing with Compound Messages and MessageFormat

Dreamweaver SPRY Custom Validation Format

I want a textfield to validated using dreamweaver spry option. I need the input to be in the below mentioned format
05632-25252525
The first (05632) part should contain minimum 3 no's, maximum upto 6.
The second (05632) part should contain minimum 7 no's, maximum upto 10.
how can i write a regular expression or a pattern in Spry. pls suggest
Spry can't handle that as all one text field.
However, if you make it two textfields, then it's simplicity...just set the first one to have min 3 character, max 6 only allow numbers. Set the second to be min7 max10 all numbers and then just combine them after the form is posted.

Resources