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.

kingasaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.