This question already has answers here:
Removing trailing zeros from BigDecimal in Java
(6 answers)
Closed 3 years ago.
I am sending Bigdecimal numbers in json object. I have an issue with zeros.
I tried stripTrailingzeros() functions but it is not working as expected
30.000 -> 3E+1 expectaion is 30
Any suggestions?
Suppose the number in BigDecimal val = 3E+1 (That is val = new BigDecimal("30.000").stripTrailingZeros();).
When you print this then out would be 3E+1.
In order to make it plain string toPlainString will help you.
System.out.println(val.toPlainString()); // 30
Related
This question already has answers here:
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 2 years ago.
Previously we using formatter= NumberFormat.getCurrencyInstance(Locale.US); we getting $12.50.Now we don't want $ sign so i used formatter = NumberFormat.getNumberInstance(Locale.US); but getting value 12.5 instead of 12.50
formatter.setMinimumFractionDigits(2); are used but no use.I observed that for (1-9) getting like 12.01,12.02 upto 12.09 for "12.10" it is printing "12.1".Like wise 12.21 coming normally for "12.20" getting "12.2".
actually u can archive the result with:
NumberFormat fn = NumberFormat.getNumberInstance(Locale.US);
fn.setMinimumFractionDigits(2);
System.out.println(fn.format(12.50));
You can use the String.format method as well:
String.format(Locale.US, "%.2f", value );
This question already has answers here:
How to format decimals in a currency format?
(22 answers)
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 6 years ago.
I'm trying to display a Double to two decimal places (like 0.00). I don't want to use system.out.print as the value must appear in a textArea. I am currently getting
£ 2.5
instead of
£ 2.50
My code looks sort of like this:
DecimalFormat myDecimalFormat = new DecimalFormat("#.00");
String text = myDecimalFormat.format(total);
This question already has answers here:
Large Numbers in Java
(6 answers)
Closed 6 years ago.
given here only as a form of validating.Which data type to use to store such a big number
Probably what you're looking for is BigInteger, BigDecimal if you want decimals instead.
This question already has answers here:
Remove trailing zero in Java
(12 answers)
Closed 7 years ago.
How to delete zeroes in java (android) only if there is no non-zero value following them?
The value is stored in double and then later in string, so working on these variables would be best.
Example:
I have 12.50000
I want to have 12.5
Example2:
I have 65.4030
I want to have 65.403
Try this :
String s = 12.50000;
s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 7 years ago.
For example: 23.7748884 and 344.456445 are the numbers I am working with. I am looking for the output with the format "000.0000". The desired result would be 023.7749 and 344.4564. I tried:
String.format("%.4f", 23.7748884) // Output: 23.7749, NOT OK! Desired: 023.7749
String.format("%.4f", 344.456445) // Output 344.4564, OK!
You could reach such result using something like:
String.format("%08.4f", 23.7748884); // results 023.7749
String.format("%08.4f", 344.456445); // results 344.4564