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

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

Related

Why my function is displaying a weird result when converting string to double? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I have a very weird result when I'm using my function and I think that I'm missing something with the rounding and double in java.
For example, when I provide the value 00159,300 for number and 100 for conversion I have 15930,000000000002 which is not possible!
public static String convertMultiply(String number, String conversion) {
number=number.replace(",", ".");
BigDecimal res=BigDecimal.valueOf(Double.valueOf(number)*Integer.valueOf(conversion));
res=res.stripTrailingZeros();
return res.toPlainString().replace(".", ",");
}
thanks in advance!
Double is an approximation of decimal values in Java. Instead, replace your line using double with:
BigDecimal res = (new BigDecimal(number)).multiply(new BigDecimal(conversion));

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 change a float into a String in Java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 7 years ago.
Is it possible to change a float value to a String? If possible, is it also possible to converting it to a String while rounding the number to the nearest integer?
For example if I have a float such as 2.335 then, can I change it to a String of value "2.335" or "2" (by rounding it)?
Use java Float class:
String s = Float.toString(25.0f);
if you want to round down a number, simply use the Math.floor() function.
float f = 2.9999f;
String s = Float.toString(Math.floor(f));//rounds the number to 2 and converts to String
first line rounds the number down to the nearest integer and the second line converts it to a string.
Another way of doing this is using the String.valueOf(floatNumber);
float amount=100.00f;
String strAmount=String.valueOf(amount);
To do this you can simply do
float example = 2.335
String s = String.valueOf(Math.round(example));
To convert a float to a String:
String s = Float.toString(2.335f);
Rounding can be done via
String.format("%.5g%n", 0.912385);
which returns 0.91239
For a more elaborate answer, see Round a number in Java
Your first requirement can be fullfilled with String.valueOf
float f = 1.6f;
String str = String.valueOf(f);
For roundoff you can use Math.round and not Math.floor. As Math.floor will convert 1.6 to 1.0 and not 2.0 while Math.round will roundoff your number to nearest integer.

Display as two Decimal [duplicate]

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

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