Up until recently my code was working fine on my development machine as well as on the deployment server.
Now out of the blue, the DecimalFormat does not work as expected and I am pretty sure that is after the windows 10 Creators Update.
My code is:
double x = 22.44;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(x));
Output: 22,44
Instead of 22.44
If i change it to :
double x = 22.44;
DecimalFormat df = new DecimalFormat("0,00");
System.out.println(df.format(x));
Output is: 0.22
I am using netbeans 7.4 with jdk 1.7.0_79u (64 bit)
Tried changing my jdk to 1.7.0_80u (32 bit) but made no difference.
Also changed the locale setting for Decimal Symbol and Digit Grouping Symbol but still the same problem.
Anyone with ideas on how to solve this issue?
This is likely a locale issue - your current code uses the default locale of the system, which may be done differently in Java 7 and Java 8. If you want to use a specific locale you can use:
double x = 22.44;
DecimalFormat df = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.FRANCE));
System.out.println(df.format(x));
df = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.UK));
System.out.println(df.format(x));
which outputs:
22,44 (with a comma)
22.44 (with a dot)
This will be your system locale, different countries usedifferent characters for the decimal and thousand separator.
You can set the locale in the decimal format to override your system default. Or you can change your system default.
Related
This question already has answers here:
How to change the decimal separator of DecimalFormat from comma to dot/point?
(8 answers)
Closed 3 years ago.
each solution I've found for this uses java rather than Kotlin, please can anyone help?
I've written an app and I'm just trying to translate to some European countries, however the math part is falling over as Germans, French etc use commas rather than a full stop. I can't figure out how to fix this with the solutions on here.
part of .kt file:
import java.math.RoundingMode
import java.text.DecimalFormat
...
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING
val lengthCalc = 0.01658
val length = df.format(lengthCalc)
any help appreciated thanks
EDIT: I have looked here: How to change the decimal separator of DecimalFormat from comma to dot/point? but as I said originally, all the solutions are for Java, not Kotlin
I can't try it at the moment but something like
val otherSymbols = DecimalFormatSymbols()
otherSymbols.setDecimalSeparator(',')
otherSymbols.setGroupingSeparator('.')
DecimalFormat df = DecimalFormat("#.##", otherSymbols)
df.roundingMode = RoundingMode.CEILING
val lengthCalc = 0.01658
val length = df.format(lengthCalc)
should do the trick. You need to use decimal format symbols for Europe, so a comma for the decimals and a full stop for the thousands.
An alternative to do it on a locale basis is to use NumberFormat and cast it to a DecimalFormat
val df = NumberFormat.getNumberInstance(currentLocale) as DecimalFormat
df.applyPattern("#.##")
...
df.format(lengthCalc)
I'm using a 3rd party software built in Java that displays numbers. This software is multilanguage and one of the languages that we use is Euskera (eu, eu_ES). Number format is shown wrong in this language (123,456.89 instead of 123.456,89).
Searching more in-depth and decompiling some classes I've seen that number formatting is done with DecimalFormatSymbols and DecimalFormat so I've made a junit test to see if the issue is from this 3rd party software or from java.
Locale locale = new Locale("eu");
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
String pattern = "#,###.##";
DecimalFormat decimalFormat = new DecimalFormat(pattern, decimalFormatSymbols);
String formatted = decimalFormat.format(1234567.89765);
assertEquals("1.234.567,9", formatted);
After running this test I've seen is Java who is formatting this way.
In one hand I've downloaded the last version of this 3rd party software because is open source and I could make a little workaround that worked. On the other hand, we use a version from 6 years ago that can't be upgraded because os system requirements and this version are in Sourceforge's CVS which I was unable to download.
Is there any way I can change the grouping separator and decimal separator for Euskera in Java level?
Yes, you can but it's a bit of a palaver. Essentially, you can create a custom NumberFormatProvider that does something different for eu_ES and delegates to the original provider for all other locales. You'll have to put it in a JAR with a META-INF/services/xxxx file and include it on the classpath.
See this question: Java override locale setting for specific locale
And more instructions here:
LocaleServiceProvider JavaDoc
Tutorial on the Java Extension Mechanism
When I perform a truncate using:
label.setText(String.format("%.2f", 1.2975118));
// 1,30
I get comma(,) instead of point(.) and this causes my program crash since I need to perform operation on float numbers.
How I can truncate a float and .setText with a point instead of comma?
Please be careful as String.format depend on your current Local configuration, you may not get a dot as a separator.
Prefer using String.format(java.util.Locale.US,"%.2f", floatValue);
Locale independent :
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
I have this Java code:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
DecimalFormat df = (DecimalFormat)nf;
newCurr = df.format(dCurr);
Basically, I pass in a number, say 12.344.
I want it rounded two places AND to use the Locale's default separator (either "." or ","). So, for example in some countries in Europe, I want this to be 12,34
So far with code above, I am halfway there. I get 12,344. I can't find where to place the DecimalFormat of ("#.##") so it can be rounded.
In other words, I can I incorporate DecimalFormat df=new DecimalFormat("#.##"); in the above? or do I have to find another way?
Edit: I am thinking I have to do the old way of (100.00 * var)/ 100.00 and pass that in?
The method setMaximumFractionDigit will do the work. See the rest of the available methods: http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html#setMaximumFractionDigits%28int%29
I have an app which runs the following two lines of code upon starting:
DecimalFormat decim = new DecimalFormat("#.00");
return Double.parseDouble(decim.format(totalNumberOfCredits));
When I start the app on my American phone, the value of decim.format(totalNumberOfCredits) is .00.
However, in my Google Play Developer Console, I have a dozen crashes, all of which look like this:
Caused by: java.lang.NumberFormatException: Invalid double: ",00"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:269)
at java.lang.Double.parseDouble(Double.java:295)
Is it really possible that DecimalFormat is producing a comma version of the decimal on European phones?
Is it really possible that DecimalFormat is producing a comma version of the decimal on European phones?
Yes, absolutely. That's what it's meant to do, after all:
Creates a DecimalFormat using the given pattern and the symbols for the default locale. This is a convenient way to obtain a DecimalFormat when internationalization is not the main concern.
To obtain standard formats for a given locale, use the factory methods on NumberFormat such as getNumberInstance. These factories will return the most appropriate sub-class of NumberFormat for a given locale.
Note that this isn't a matter of a "European version of Android" - it's just a matter of using Android in a context where the default locale uses , as the decimal separator.
If you want to use the symbols for a particular locale, but using a specific pattern, you could use:
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
DecimalFormat format = new DecimalFormat("#.00", symbols);
Having said that, it's not at all clear what you're trying to do in the first place - why would you format and then parse a number? You should almost always avoid string conversions when you don't really need them. Why not just convert it directly? (We don't know what totalNumberOfCredits is, which doesn't help.)
public double getTwoPointDecimal(double value) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
return Double.parseDouble(new DecimalFormat("##.##", symbols).format(value));
}
try it, its help me in my project
double unit = Float.parseFloat(String);
DecimalFormat decimal = new DecimalFormat("##.###").format(unit);
try this it help me in my project