how to set 3 range zero in double in android java [duplicate] - java

This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 4 years ago.
When I enter the number 0.0007575 on the double, the result will be "7.575E-4", but I want result 0.0007575
thanks

Use a custom DecimalFormat to format the double value:
DecimalFormat fmt = new DecimalFormat("#0.000"); // leading zero and 3 fraction digits
String result = fmt.format(11.123456789);
System.out.println(result); // 11.123

Related

How would I make a double value have a trailing zero in java. My program deals with currency [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Round a double to 2 decimal places [duplicate]
(13 answers)
What data type to use for money in Java? [closed]
(11 answers)
Closed 3 years ago.
How would I make a double value have a trailing zero in java. My program deals with currency.
Ex. How do I to make 9.5 to 9.50?
For currencies, it would be better to use BigDecimal class which doesn't lose precision. You can define the precision as scale parameter.
If you want to display 9.50 to the user, use
String.format("%.2f", decimalValue)
where 2 is the number of decimal places you want to display.
Assuming that you need to convert a double to a String with two decimal places, you have to use a DecimalFormat:
DecimalFormat df = new DecimalFormat("#0.00");
double doubleValue = 9.5d;
String stringValue = df.format(doubleValue); // "9.50"
But when you are dealing with currency, it's better to deal with integers/longs/BigIntegers of the lowest possible amount (1 cent, 1 satoshi, etc) to avoid floating point errors, so keep that in mind.
If you're dealing with a currency, use the currency and internationalisation features of Java:
public static void main(String[] args) {
double iii = 9.5;
NumberFormat format = NumberFormat.getCurrencyInstance();
NumberFormat formatUS = NumberFormat.getCurrencyInstance(Locale.GERMANY);
System.out.println("Format currency (default) : " + format.format(iii));
System.out.println("Format currency (Germany) : " + formatUS.format(iii));
}
This produces:
Format currency (default) : $9.50
Format currency (Germany) : 9,50 €

I want to print amount like 2523525252.025 when I using double data type it's changed in 2.523525252025E9 how can fix? [duplicate]

This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 5 years ago.
I want to print amount like 2523525252.025 when I using double data type it's changed in 2.523525252025E9 how can fix?
Example -
Double saleprice=145236.12;
int itemqountity=1500;
Double totalamount= saleprice*itemqountity;
Log.e("totalamount",""+totalamount);
output-
E/totalamount: 2.1785418E8
Please use DecimalFormat to print your value without scientific notation here is an example.
double test = 12345678;
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
System.out.println(df.format(test));
//12345678
BigDecimal bigDecimal = new BigDecimal(2.523525252025E9);
System.out.println(bigDecimal.toPlainString());

Rounding to one decimal place? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 5 years ago.
I have the following variables initialized to the user inputs,
int completedPasses = reader1.nextInt();
int attemptedPasses = reader2.nextInt();
double completionRatio = (250 / 3) * ((completedPasses / attemptedPasses) - 0.3);
How do I round the computation above to a double of one decimal place?
Use DecimalFormat:
DecimalFormat df = new DecimalFormat("#.#");
df.format(completionRatio);
Or Math.round:
Math.round(completionRatio);

i want to round a double variable into two decimals only [duplicate]

This question already has answers here:
Round a double to 2 decimal places [duplicate]
(13 answers)
Closed 8 years ago.
how can i round a double variables in java into only two decimals?
yourNewFormatedDouble = new DecimalFormat("#.##").format(yourOldDoubleHere);
DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(myDouble);

Format double to 2 decimal places [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Round a double to 2 decimal places [duplicate]
(13 answers)
Closed 9 years ago.
I am trying to Format my double value to exact 2 decimal places and it seems to working fine, here is the code i am trying
final NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
df.setRoundingMode(RoundingMode.DOWN);
df.format(value)
Till now everything is working, but i need to return double value as it is being used for other calculations and i tried
Double.parseDouble(df.format(value))
with big decimal like
BigDecimal price = new BigDecimal(df.format(number));
but it is not working as expected
like 18.50 is being converted as 18.5
Though this is not an issue with calculations but i need to show amount on the UI where i have to show exactly up to 2 decimal places.
Is there any was i can handle it in java class or i have to take care in JSP with JSTL
This is what BigDecimal is made for!
BigDecimal number = new BigDecimal(123.456);
// set 2 fraction digits
// Note that setScale() does not change the original,
// but returns a new BigDecimal.
number = number.setScale(2, RoundingMode.DOWN);
// get string representation
String text = number.toPlainString();
// get double value
double dbl = number.doubleValue();
And use BigDecimal for other calculations as well if you can.

Categories

Resources