DecimalFormat not formatting my numbers correctly - java

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#");

Related

Set precision using decimalFormat error - 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

Problems with Decimalformat

Hey i am using the following code to format my numbers to have two decimal places
public String format(double num)
{
String temp="";
DecimalFormat df=new DecimalFormat("R.##");
temp=temp+df.format(num);
return temp;
}
When I print out the results it gives me the answeres with only one decimal place.
Please can someone help me stop that.
You use # in DecimalFormat which is the character that says "print the digit. if it is 0 leave it out."
You need to use 0 where 0 is also printed as 0.
E.g.
DecimalFormat df=new DecimalFormat("R.00");
BigDecimal bd = new BigDecimal(123.12331312);
System.out.print(bd.setScale(2,1));
try this
Use following way to format
System.out.printf("%.2f",num);

Formatting numbers using DecimalFormat

I am trying to format prices using DecimalFormat, but this isn't working for all variations.
DecimalFormat df = new DecimalFormat("0.##")
df.format(7.8)
df.format(85.0)
prints
7.80
and
85
but "7.79999" gets formatted as "7.8", not "7.80". I have tried doing things this way
DecimalFormat df = new DecimalFormat("0.00")
to force two dp, but then "85.0" gets formatted as "85.00" not "85"!
Is there a way of capturing all variations, so that prices are printed either as #, ##, or #.##? For example:
5, 55, 5.55, 5.50, 500, 500.40
There is a slight difference between these two formats. The "#.##" means it will print the number with maximum two decimal places whereas "#.00" means it will always display two decimal places and if the decimal places are less than two, it will replace them with zeros. see the example below with output.
public static final DecimalFormat df1 = new DecimalFormat( "#.##" );
public static final DecimalFormat df2 = new DecimalFormat( "#.00" );
System.out.println(df1.format(7.80));
System.out.println(df1.format(85));
System.out.println(df1.format(85.786));
System.out.println(df2.format(7.80));
System.out.println(df2.format(85));
System.out.println(df2.format(85.786));
And the output will be
7.8
85
85.79
7.80
85.00
85.79
This doesn't seem to be solved by a single formatter. I suggest you use "0.00" format and replace ".00" with an empty string.
public static String myFormat(double number) {
DecimalFormat df = new DecimalFormat("0.00");
return df.format(number).replaceAll("\\.00$", "");
}
I don't think it's possible, at least not with Java SE formatters. You need to make a custom formatter. I would do it like this
String res = df.format(number).replace(".00", "");
Use the BigDecimal number class instead:
e.g. if n is a BigDecimal,
then you can use
String s = NumberFormat.getCurrencyInstance().format(n);
By the way, it's best practice to use BigDecimal when working with money.
You can try with:
DecimalFormat df = new DecimalFormat("#.##",new DecimalFormatSymbols(Locale.US));
System.out.println(new java.text.DecimalFormat("#.##").format(5.00));
This will print 5
System.out.println(new java.text.DecimalFormat("#.00").format(500.401));
This will print 500.40

Decimal conversion

I am trying to set decimal values,Below is my input string
String rate="1.000000000";
Converting to double:
Double converted=Double.valueOf(rate);
DecimalFormat format=new DecimalFormat("#.########"); //Setting decimal points to 8
System.out.println("ouput"+format.format(rate)); //Giving output as 1.
I dont understand how to do this,Any hints please.
Regards,
Chaitu
Try
DecimalFormat format=new DecimalFormat("#.00000000");
and
System.out.println("ouput"+format.format(converted));
# will not be displayed for 0, use 0 instead:
String rate="1.010000000";
Double converted=Double.valueOf(rate);
DecimalFormat format=new DecimalFormat("0.00000000");
System.out.println("ouput "+format.format(converted));
Firstly, you're passing the string rate to the DecimalFormat.format method. This will fail, you need to pass the converted object in.
When I tested your code with the above changes, I got 1.01 in the output. To format to 8 decimal places, follow Bala Rs comments. i.e. DecimalFormat format = new DecimalFormat("#.00000000");

Java decimal formatting using String.format?

I need to format a decimal value into a string where I always display at lease 2 decimals and at most 4.
So for example
"34.49596" would be "34.4959"
"49.3" would be "49.30"
Can this be done using the String.format command?
Or is there an easier/better way to do this in Java.
Yes you can do it with String.format:
String result = String.format("%.2f", 10.0 / 3.0);
// result: "3.33"
result = String.format("%.3f", 2.5);
// result: "2.500"
You want java.text.DecimalFormat.
DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(34.4959);
Here is a small code snippet that does the job:
double a = 34.51234;
NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(4);
df.setRoundingMode(RoundingMode.DOWN);
System.out.println(df.format(a));
java.text.NumberFormat is probably what you want.
NumberFormat and DecimalFormat are definitely what you want. Also, note the NumberFormat.setRoundingMode() method. You can use it to control how rounding or truncation is applied during formatting.
You want java.text.DecimalFormat

Categories