Asked  6 Months ago    Answers:  5   Viewed   42 times

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



No, there is no better way.

Actually you have an error in your pattern. What you want is:

DecimalFormat df = new DecimalFormat("#.00"); 

Note the "00", meaning exactly two decimal places.

If you use "#.##" (# means "optional" digit), it will drop trailing zeroes - ie new DecimalFormat("#.##").format(3.0d); prints just "3", not "3.00".

Tuesday, June 1, 2021
answered 6 Months ago

There is an issue already filed about this at the OpenJFX docs.

While it hasn't been resolved yet, there is a possible workaround, based on:

NetBeans only adds javadoc/source jars for a jar with the exact same name and -javadoc/-source suffix

So here are the steps to solve it:

  • Install NetBeans 10 and JDK 11.0.2.

  • Clone the HelloFX sample for NetBeans and Maven, from the OpenJFX samples.

  • Update the JavaFX dependencies to 11.0.2.

  • Run it:

    mvn clean compile exec:java
  • Check that the JavaFX dependencies have been downloaded to your local m2 repository. Under <user home>/.m2/repository/org/openjfx/javafx-base/11.0.2 for instance you will find javafx-base-11.0.2.jar and javafx-base-mac-11.0.2.jar (or win, or linux based on your platform).

  • Back on NetBeans, right click in the Dependencies folder and select Download Sources (see the task progress in the bottom right taskbar), and then Download Javadoc(see the task progress).

  • Go to your m2 repository and verify that there are now -source and -javadoc jar files.

However, this won't solve the issue yet, there is an extra step:

  • In your m2 repository, manually rename the -source and -javadoc jar files using your platform classifier, to -mac-source and -mac-javadoc (or win, or linux based on your platform). Do this for the different JavaFX modules:

Back to NetBeans, check that now you have JavaDoc, or if you press Ctrl/CMD+Click you can access the source.

Note that this fix has to be done only once, the rest of your Maven projects should pick JavaDoc and Sources.

Thursday, August 5, 2021
answered 4 Months ago

if you are going to step down, then change your project's source to 1.7 as well,

right click on your Project -> Properties -> Sources window 

and set 1.7 here

note: however I would suggest you to figure out why it doesn't work on 1.8

Saturday, September 4, 2021
answered 3 Months ago

If you are sure that the double is indeed an integer use this one:

NumberFormat nf = DecimalFormat.getInstance();
String str = nf.format(documentNumber);

As a bonus, this way you keep your locale's configuration as in thousand separator.

I add this previously removed option as it seems that was useful to the OP:

Tuesday, October 19, 2021
answered 1 Month ago

Use sprintf formatting with NSString: [NSString stringWithFormat: @"%.2f", myValue];

Thursday, November 25, 2021
answered 4 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :