Double decimal formatting in Java

0 votes

I'm having some problems formatting the decimals of a double. 

If I have a double value, e.g. 5.0, how do I format the decimals so that it is 5.00 instead?

Aug 23, 2018 in Java by samarth295
• 2,220 points
399 views

1 answer to this question.

0 votes

Using String.format, you can do this:

double price = 52000;
String.format("$%,.2f", price);

Notice the comma which makes this different from @Vincent's answer

Output:

$52,000.00

A good resource for formatting is the official java page on the subject

answered Aug 23, 2018 by Parth
• 4,630 points

Related Questions In Java

0 votes
1 answer

Formatting Double Value in Java

An alternative is to use String.format: double[] arr = ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,620 points
2,322 views
0 votes
3 answers

String to Double conversion in java

Double temp = Double.valueOf(str); number = temp.doubleValue(); READ MORE

answered Sep 10, 2018 in Java by Sushmita
• 6,910 points
1,334 views
0 votes
4 answers

How can we display an output of float data with 2 decimal places in java? Please help

You can use DecimalFormat. One way to use ...READ MORE

answered Dec 16, 2020 in Java by Gitika
• 65,910 points
160,855 views
0 votes
1 answer

How do you produce a double in Java

double num = 5; When you do this, ...READ MORE

answered Oct 4, 2018 in Java by anto.trigg4
• 3,440 points
579 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,515 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
7,956 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
16,987 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,384 views
0 votes
2 answers

How to round any number to n decimal places in Java?

new BigDecimal(String.valueOf(double)).setScale(yourScale, BigDecimal.ROUND_HALF_UP); will get you a BigDecimal. To ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 59,230 points
1,295 views
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,120 points
2,176 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP