[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 932
  • Last Modified:

float scale

Hello,

I have a float # 1234.12345 , how can set the scale for only 2 digits after the point
1234.12
   
     thank,  ASA.

0
kingasa
Asked:
kingasa
1 Solution
 
_lychee_Commented:
u can use NumberFormat, which is in java.text.... something like this:

NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(1234.12345));
0
 
heyhey_Commented:
or you can do it by hand  :)

double l = 3248.298475;
l = ((double)((int)(l * 100))) / 100;
0
 
mksaloCommented:
.. Or you can use DecimalFormat, which is an extension of NumberFormat.

You can find it from java.text.DecimalFormat.

You can define a pattern according to which the formatting takes place. As in your question, to always format to two decimal (and a zero before decimal separator) even if there is only one (for ex. 123.5 -> 123.50) do this:

DecimalFormat df = new DecimalFormat("0.00");
double d = 12345.56789;

System.out.println(df.format(d));
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now