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));
Related
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.
I'm importing data from MS SQL Server with Java and JDBC. The data in my database have 2 fractional digits.
The MONEY datatype in SQL Server has four digits of precision, so what you're seeing is what should be expected. You can use DECIMAL if you only want two digits of precision.
Your front end should be able to display the data however it wants to display it though.
Completely agree with the explanation from Tom H. If you want to format data on the user side you can use DecimalFormat or to be more precise as it is currency try using NumberFormat's currency instance.
DecimalFormat df = new DecimalFormat("##.00");
String result = df.format(Double.parseDouble((data)));
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
String result = nf.format(new BigDecimal(data));
You should change the Locale according to your requirement. FYI, using currency instance will return value with corresponding currency sign from Locale.
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
I am using Poi to create Excel workbooks in Java. My raw data comes in as a string. I need to format the data to enter two decimal places into the cell where the number is being written. I use Double.parseDouble() to convert the string to numeric and then use DecimalFormat to format the numeric as a string. Another call to Double.parseDouble() to return the value to numeric (the cell where it is going is formatted numeric, so I can't use the string value) and I should be good. Problem is, that second call to Double.parseDouble() truncates any trailing zeroes off from the right of the decimal point. Anybody have an idea as to how I can coerce this value to read as, say, 1.50 rather than 1.5?
I always want two decimals.
Solution: Always apply specific decimal format pattern.
Sample code snippet:
//java.text.DecimalFormat df = new java.text.DecimalFormat( "###0.00" );
java.text.DecimalFormat df = new java.text.DecimalFormat();
df.applyPattern( "###0.00" ); // always two decimals
double dbl = 1.50d ;
// prints: dbl = 1.5
System.out.println( "dbl = " + dbl );
// prints: df.format( 1.5 ) = 1.50
System.out.println ( "df.format( " + dbl + " ) = " + df.format( dbl ) );
UPDATE:
OK, from your posting, I understand that you are trying to fill the numeric formatted cell only to print or show up with two decimal positions. You know by default all numeric fields are interpreted omitting trailing zeros. To achieve your requirement, you may require to use CellFormat and/or DataFormatter on your contextual Cell object, but when said Format, it is a String again.
I didn't try the following code but may help you.
DataFormatter dataFormatter = new DataFormatter();
dataFormatter.setDefaultNumberFormat( instanceOfDesiredDecimalFormat );
// or
// dataFormatter.setExcelStyleRoundingMode( instanceOfDesiredDecimalFormat );
// apply this format on the cell you want
dataFormatter.formatCellValue( instanceOfNumericCellInContext );
You are actually doing nothing in most part of the code you described. You might as well just return Double.parseDouble(inputString). Doubles are stored in binary format and the leadin/trailing zeros make no sense. Perhaps the BigDecimal class is something for you.
It appears we are at an impasse. As Mario pointed out, doubles are managed as binary and there is no way to format the binary as a double, except to convert it to a string with DecimalFormat, which is no longer a double. I explained this to my boss and he's ok with the solution of taking the raw double, so I'm closing this issue. Thanks to all for your help and support.
regards,
Mike