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));
Related
This question already has answers here:
Add leading zeroes to number in Java? [duplicate]
(5 answers)
Closed 4 years ago.
I need a string like 50 to appear as 050.0. I am using String.format, but I can't figure out how to do leading zeros and a single decimal place at the same time. So far, I have tried String.format("%3.2f", number);, but that isn't working as I still get 50.0 rather than 050.0
Use DecimalFormat to control the number of mandatory digits:
DecimalFormat df = new DecimalFormat("#000.0");
System.out.println(df.format(50)); // 050.0
where
Symbol Location Localized? Meaning
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
You can use StringBuilder class to create a string with number 0 and then append it with you number and insert the decimals at the end.
int num = 50; /*Your number*/
StringBuilder s_num = new StringBuilder("0");
s_num.append(num);
s_num.append(".0");
String f_num = s_num.toString();
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Why not use Double or Float to represent currency?
(16 answers)
Closed 5 years ago.
I need to multiply a float by 100 in order to convert from € to cents. The problem I am having is, that the value of big numbers isn't calculated correctly.
For example:
String aa = "1000009.00";
aa = aa.replaceAll(",", ".");
float bb = Float.parseFloat(aa);
bb=Math.round(bb*100);
System.out.println(bb);
What I am getting is: 1.00000896E8
So I know this is because of the way float in Java works.
I have searched for an answer but people always say "use Math.round()" which doesn't work for me.
What can i do to prevent this?
You can use double for more precision (or BigDecimal if you expect to work with very big numbers).
String aa = "1000009.00";
double bb = Double.parseDouble(aa);
bb=Math.round(bb*100);
System.out.printf("%.2f", bb); // it prints only two digits after the decimal point
Output
100000900.00
You can use BigDecimal::multiply for example :
String aa = "1000009.00";
aa = aa.replaceAll(",", ".");
BigDecimal fullValue = new BigDecimal(aa);
System.out.println("full value = " + fullValue.multiply(new BigDecimal(100)));
Output
full value = 100000900.00
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);
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
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.