I have some problems with pattern for Double

Hi,
I have some problems with pattern for Double, for English the next code work:

          if (id_depositField.getValue().getHumidity() == null) {
                    Humidity = "";
                } else Humidity = id_depositField.getValue().getHumidity().toString();
                dHumidityField.setValue(Humidity);

id_depositField.getValue().getHumidity() is Double
and
dHumidityField is a textField
so take from database a float value and I inserted to textFiled.
In database the float values have (.) dot for decimal after covert to string put also dot but when is saved the dot disappear because I used Romania locale where I defined for decimal the comma (,) and for thousand separator the dot (.)
My numbers from 6.5 is take it sometime 65 or 6.5 and saved 65, for example.
I have defined my pattern for Romanian.

                integerFormat=#
                numberDecimalSeparator=,
                numberGroupingSeparator=.

I try to use others method to transfer to string:

               if (id_depositField.getValue().getHumidity() == null) {
                    Humidity = "";
                } else Humidity = String.valueOf(id_depositField.getValue().getHumidity());
                dHumidityField.setValue(Humidity);

and

                if (id_depositField.getValue().getHumidity() == null) {
                    Humidity = "";
                } else Humidity = Double.toString(id_depositField.getValue().getHumidity());
                dHumidityField.setValue(Humidity);

but also not work.

I try to define the pattern for double:

                doubleFormat=#.##0,00

but after this I got next error:
IllegalArgumentException: Malformed pattern "#.##0,00"

finally I found a solution:

                if (id_depositField.getValue().getHumidity() == null) {
                    Humidity = "";
                } else Humidity=numberFormatter.apply(id_depositField.getValue().getHumidity());
                dHumidityField.setValue(Humidity);

with this is add correctly to dHumidityField.

But how is necessary to defined the pattern for Double in doubleFormat=

Hello Florin.

You have found a correct solution via NumberFormatter.

About formats:

  • If you want to change separators - set the proper values to numberDecimalSeparator and numberGroupingSeparator as you have done. These changes will be applied to default formats.
  • If you want to change format itself (e.g. double) - you should set new format in doubleFormat. But you have to specify value the “original way” without separators swap (‘comma’ as a group separator and ‘dot’ as a decimal separator). Otherwise you will face the IllegalArgumentException: Malformed pattern because this format has static syntax.

Regards,
Ivan

1 Like