Display as two Decimal [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a number to 2 decimal places in Java
I need to display a decimal number up to two digits in Java.
For example:
Case1. 2.333 - 2.33
Case2. 3.4 - 3.40
I am able to do the first case. Can anybody help me how to do for the second case.

If you just want to print a double with two digits after the decimal point, use something like this:
double value = 200.3456;
System.out.printf("Value: %.2f", value);
If you want to have the result in a String instead of being printed to the console, use String.format() with the same arguments:
String result = String.format("%.2f", value);
Or use class DecimalFormat:
DecimalFormat df = new DecimalFormat("####0.00");
System.out.println("Value: " + df.format(value));

You can try
System.out.printf("%.2f %.2f%n", 2.333, 3.4);
prints
2.33 3.40

Related

Printing out double with variable length after decimal point [duplicate]

This question already has answers here:
Use DecimalFormat to get varying amount of decimal places
(4 answers)
Closed 1 year ago.
I was searching for a solution of how to print double with variable length. Means: user will define how many digits he wants after the decimal point, but without success.
I've come to something like, but it doesn't work :
num - double
dec(length) - integer
System.out.printf("%.(%d)f\n", num, dec);
Are you looking for something like the following?
Maybe solution 2 is suitable for you.
Solution 1:
System.out.printf("%.2f", val); // "%.2f" it's a string so you can make it in several ways...eg: "%."+ dec + "f";
Solution 2:
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2); // you can use int variable instead of 2.. eg: df.setMaximumFractionDigits(dec);
System.out.println(df.format(decimalNumber));

How to format a Double coming from a Object in Java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I want to format a Double obtained from an Object so it only displays three digits after the decimal point. Here's the current code:
Three a = Data.get(index);
// Get the y-axis acceleration value
double b = a.getY();
String accelerationOutUnfiltered = Double.toString(b);
Data[0] = accelerationOutUnfiltered;
Note: I am doing this in Android, and when I use String.format("%.3f", y) this doesn't work and it throws me a error in Android Studio. Currently the above code works but it displays 15 digits after the decimal point.
I have tried several forms, but they all have failed. Please help. Thanks :)
You could use DecimalFormatter.
For example:
double b = a.getY();
DecimalFormat formatter = new DecimalFormat("#.###");
formatter.setRoundingMode(RoundingMode.CEILING);
String formattedDouble = formatter.format(b);
Have tried decimal formatter?
You do something like this:
DecimalFormat df = new DecimalFormat("#.#####");
df.format(<your_number>);

how to format string to show two decimal places [duplicate]

This question already has answers here:
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 6 years ago.
I am facing a slight issue when trying to get two decimal places after pasring double to string and trying to format
pieChart.setCenterText("$" + "" + "" +String.format( "% 1$ .2f", Double.toString(dataCost),""));
can anyone help me improve the above line of code so that it can display to two decimal places? You will also notice that I am trying to leave a space between the dollar sign and the value
You can use String.format("%.2f", d) , your double will be rounded automatically
pieChart.setCenterText("$ " + String.format("%.2f", d));
Following code might help you
double a = 1.234567;
double a = 2;
NumberFormat nf = new DecimalFormat("##.##");
System.out.println(nf.format(a));
System.out.println(nf.format(a));
and the output will be
1.23
2
it only show decimal places if needed, Enjoy! :)
Try Like This
pieChart.setCenterText("$ " + String.format("%.2f", dataCost));
You can use DecimalFormat.
import java.text.DecimalFormat;
DecimalFormat money = new DecimalFormat ("$0.00");
System.out.println(money.format(dataCost));
Try this.
float val = 1245.235645f;
double ans = Double.parseDouble(new DecimalFormat("##.##").format(val));
System.out.println(ans);
Note : ##.## means 2 digits will be displayed after the .(dot)
Package : import java.text.DecimalFormat;
this should helps you.

How to get exact decimal value from bigdecimal in java [duplicate]

This question already has answers here:
"new BigDecimal(13.3D)" results in imprecise "13.3000000000000007105.."?
(5 answers)
Closed 6 years ago.
I am trying to get number of digits after decimal point in BigDecimal value.
BigDecimal big = new BigDecimal(1231235612.45);
String[] str = big.toPlainString().split("\\.");
System.out.println(" Decimal Value: " + str[1]);
Using this I am getting following output -
Decimal Value: 4500000476837158203125.
Actualy I want to display only 45 as per the original BigDecimal value (1231235612.45).
So, my expected output is Decimal Value: 45.
But, while conversion it adds more digits after decimal points.
Is there any method or code to get exact same value from BigDecimal?
Don't use the double Constructor of BigDecimal (See Javadoc, it is discouraged).
use String constructor
new BigDecimal("1231235612.45");
or use MathContext
new BigDecimal(1231235612.45, MathContext.DECIMAL64);

Best way to Format a Double value to 2 Decimal places [duplicate]

This question already has answers here:
Round a double to 2 decimal places [duplicate]
(13 answers)
Closed 3 years ago.
I am dealing with lot of double values in my application, is there is any easy way to handle the formatting of decimal values in Java?
Is there any other better way of doing it than
DecimalFormat df = new DecimalFormat("#.##");
What i want to do basically is format double values like
23.59004 to 23.59
35.7 to 35.70
3.0 to 3.00
9 to 9.00
No, there is no better way.
Actually you have an error in your pattern. What you want is:
DecimalFormat df = new DecimalFormat("#.00");
Note the "00", meaning exactly two decimal places.
If you use "#.##" (# means "optional" digit), it will drop trailing zeroes - ie new DecimalFormat("#.##").format(3.0d); prints just "3", not "3.00".
An alternative is to use String.format:
double[] arr = { 23.59004,
35.7,
3.0,
9
};
for ( double dub : arr ) {
System.out.println( String.format( "%.2f", dub ) );
}
output:
23.59
35.70
3.00
9.00
You could also use System.out.format (same method signature), or create a java.util.Formatter which works in the same way.

Categories