Set precision using decimalFormat error - Java - java

I am attempting to use the DecimalFormat java class for the first time, and I am running into a strange issue. I would like 125.295 to round to 125.30. I would think the format should automatically include the 0, but maybe I'm incorrect.
double num = 125.295;
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.HALF_UP);
String str = df.format(num);
System.out.println(str); //this is yielding 125.3 instead of 125.30
Please help and thank you in advance!

The DecimalFormat class treats '#' as "hide trailing zeroes" or '0' as "show the zeroes". As per the API for DecimalFormat:
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
So you should use DecimalFormat("#.00") instead of DecimalFormat("#.##") if you want it to show trailing zeroes.

Try to use DecimalFormat like this:
double num = 125.295;
DecimalFormat df = new DecimalFormat("###.00");
System.out.println("sum (DecimalFormat) : " + df.format(num));
Output:
125.30

Related

How to round and scale a number using DecimalFormat

I'm trying to to format a number using the DecimalFormat but the problem that I didn't get the expected result. Here is the problem:
I have this number: 1439131519 and I want to print only the five first digits but with a comma after 4 digits like this: 1439,1. I have tried to use DecimalFormat but it didn't work.
I tried like this but it dosen't work:
public static DecimalFormat format2 = new DecimalFormat("0000.0");
Anyone have an idea?
It is easier to do with maths rather than formatting.
Assuming your number is in a double:
double d = 1439131519;
d = d / 100000; // d = 14391,31519
d = Math.round(d) // d = 14391
d = d / 10; // d = 1439,1
Of course, you can do it in one line of code if you want. In using Math.round I am assuming you want to round to the nearest value. If you want to round down you can use Math.floor.
The comma is the normal decimal separator in much of Europe, so that might work by default in your locale. If not, you can force it by getting the formatter for a locale such as Germany. See: How to change the decimal separator of DecimalFormat from comma to dot/point?.

Convert Int value to Decimal $

I've already tried a way to format this whole number in decimal plus no solve the one that comes closest to the result hoping was using the BigDecimal plus it was very extensive. Where am I going wrong?
Double value = 20852;
DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(value);
result = 20852.00
expected outcome : 20.852
With decimal format you define how many decimal places are need to be set. The output is completely correct. It would create the desired output if you have double value = 20.852. What you want is to set the thousand sepeator:
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
DecimalFormat df = new DecimalFormat("###,###.##", symbols);
As stated by your question, without changing anything to your number and to get the result your expressively required, you could do this with setting the dot character as a thousands separator :
Double value = 20852d;
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat("##,000", symbols);
String result = df.format(value);

Formatting string number into double variable with two number after decimal point

I'm getting from server a string value formatted as follow: 14.5000
I need to create a double variable from it with two number after decimal point: 14.50. I've tried the following:
DecimalFormat df = new DecimalFormat("#,00");
Double priceD = Double.parseDouble((produitParam.item(paramNb).getTextContent()));
String dx = df.format(priceD);
produit.setPrixTtc(Double.valueOf(dx));
And I'm getting 14.5. If I use DecimalFormat("#.00"), it gives me 15...
Someone could help me with that ?
If you want string with precision upto 2 points after decimal you should use
DecimalFormat df = new DecimalFormat("#.00");
you have used "#,00"
',' is used for specifying grouping Separator.
for more information here is the Java Doc of DecimalFormat:
http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
you should look this link.There is a lot of answer your question.
I think.The best answer is DecimalFormat df = new DecimalFormat("#0.00");
for you in link
[how to convert double to 2 number after the dot?

Convert a number to 2 decimal places in Java

I want to convert a number to a 2 decimal places (Always show two decimal places) in runtime. I tried some code but it only does, as shown below
20.03034 >> 20.03
20.3 >> 20.3 ( my code only rounds not converts )
however, I want it to do this:
20.03034 >> 20.03
20.3 >> 20.30 (convert it to two decimal places)
My code below:
angle = a variable
angle_screen = a variable
DecimalFormat df = new DecimalFormat("#.##");
angle = Double.valueOf(df.format(angle));
angle_screen.setText(String.valueOf(angle) + tmp);
Any help on how to do this would be great, thanks.
try this new DecimalFormat("#.00");
update:
double angle = 20.3034;
DecimalFormat df = new DecimalFormat("#.00");
String angleFormated = df.format(angle);
System.out.println(angleFormated); //output 20.30
Your code wasn't using the decimalformat correctly
The 0 in the pattern means an obligatory digit, the # means optional digit.
update 2: check bellow answer
If you want 0.2677 formatted as 0.27 you should use new DecimalFormat("0.00"); otherwise it will be .27
DecimalFormat df=new DecimalFormat("0.00");
Use this code to get exact two decimal points.
Even if the value is 0.0 it will give u 0.00 as output.
Instead if you use:
DecimalFormat df=new DecimalFormat("#.00");
It wont convert 0.2659 into 0.27. You will get an answer like .27.
Try this: String.format("%.2f", angle);
Try
DecimalFormat df = new DecimalFormat("#,##0.00");

DecimalFormat not formatting my numbers correctly

I want to round all of my numbers to 1 decimal place.
For example
22.0
-6.1
I am using:
DecimalFormat decimalFormat = new DecimalFormat("0.0");
middlePanelTextView.setText(decimalFormat.format(score.getElevationAngle()));
But whole numbers don't have a 0 appended to it.
-18 should be -18.0 etc.
instead my value shows up as -18.
.# represents a significant digit 0 doesn't.
.# represents and optional digit So try "###.#"
http://developer.android.com/reference/java/text/DecimalFormat.html
I'm not sure about the syntax: but the above article should give you all you need.
Try
middlePanelTextView.setText(""+decimalFormat.format(score.getElevationAngle()));
My TextView was cutting off the digits. My fault. Shrank font size.
Try using
DecimalFormat decimalFormat = new DecimalFormat("#.#");
as the constructor parameter instead.
Try using the following code:
DecimalFormat decimalFormat = new DecimalFormat("0.0#");

Categories