Formatting Double Value in Java

0 votes

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
May 15, 2018 in Java by developer_1
• 3,320 points
2,299 views

1 answer to this question.

0 votes

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.Formatterwhich works in the same way.

answered May 15, 2018 by Rishabh
• 3,620 points

Related Questions In Java

0 votes
1 answer

Double decimal formatting in Java

Using String.format, you can do this: double price ...READ MORE

answered Aug 23, 2018 in Java by Parth
• 4,630 points
387 views
0 votes
1 answer

Arrays are passed by value or passed by reference in Java?

Arrays are not a primitive type in ...READ MORE

answered Jul 18, 2018 in Java by sophia
• 1,400 points
9,581 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
548 views
0 votes
1 answer

How to get an enum value from a string value in Java?

Hello @kartik, Yes, Blah.valueOf("A") will give you Blah.A. Note that the name ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,880 points
1,735 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
1,874 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,620 points
886 views
0 votes
4 answers

Remove extra spaces from string in java

import java.util.regex.Matcher; import java.util.regex.Pattern; String pattern="[\\s]"; String replace=""; part="name=john age=13 year=2001"; Pattern ...READ MORE

answered Sep 10, 2018 in Java by Parth
• 4,630 points
2,634 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,146 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,304 views
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
828 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