formatting a string to two decimal places [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to round a number to n decimal places in Java
I am trying to format a string that is used as currency to two decimal places. For example, if they enter 100, it will format it to 100.00, and if they enter 100.5, it will be formatted to 100.50. What would be the best way to go about this?

You can use DecimalFormat:
new DecimalFormat("###0.00").format(...);
Other constructors of DecimalFormat introduces support to Locale.

Related

Format a float to 2 decimal places without changing variable type [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I want to take a float that can be entered by the user to any acceptable number of decimal places and format it so that it is only two decimal places. I am able to do this by converting it to a String but I want to keep it as a float and was wondering if there was an easier way to do this than changing from float to string and then back to a float?
Here's a way to do it:
https://stackoverflow.com/a/153785/4119650
It's a little more complicated than the String conversion method, but it gives you greater control over the truncation.

How to format BigDecimal to ALWAYS have two decimal places? [duplicate]

This question already has an answer here:
Rounding BigDecimal to *always* have two decimal places
(1 answer)
Closed 6 years ago.
This:
BigDecimal.valueOf(0.00)
becomes 0.0
I want it to be 0.00
What is the correct format pattern for that?
Use the String constructor: new BigDecimal("0.00"). Using BigDecimal.valueOf(double) completely destroys any formatting you used to input the value.
Try this
value = value.setScale(2, RoundingMode.CEILING)

Remove Decimal from BigDecimal [duplicate]

This question already has answers here:
Java BigDecimal remove decimal and trailing numbers
(6 answers)
Closed 6 years ago.
I have a BigDecimal number, I just want to remove the decimal numbers from it,
for example if I have 200.88 then output should be 200?
I tried Bigdecimal rounding function but they wont do the job
You can specify the rounding mode to ROUND_FLOOR when you use round.

Java - 100 decimal places in variable [duplicate]

This question already has answers here:
How many significant digits do floats and doubles have in java?
(6 answers)
Closed 7 years ago.
I have a problem when it comes to calculating pi with more than 15 decimal places(i used double).
My result looks quite good except my variable is limited to 15 decimal places:
3.140592653839794
Anybody could tell me what i have to do if i want more decimal places?
Thanks and Greeting!
Use java.math.BigDecimal instead of double for arbitrary (finite) precision.

Format floating point number with leading zeros [duplicate]

This question already has answers here:
printf how to do floating points with leading zeros
(3 answers)
Closed 7 years ago.
Why does
System.out.format("%03.3f", 1.23456789);
print 1.235 instead of 001.235?
How has my format string to look like to get 001.235 as output of the following code line?
System.out.format(format, 1.23456789);
Number after %0 here defines full width including decimal point, so you need to change it to 7:
System.out.format("%07.3f", 1.23456789);
DecimalFormat formatter = (DecimalFormat)NumberFormat.getNumberInstance(Locale.US);
formatter.applyPattern("000.###");
System.out.format(formatter.format(1.23456789));
Result:
001.234
Demo

Categories

Resources