Avatar of rajuvegesna
rajuvegesna
 asked on

Math functions

hai,

I have a calculated double value for ex. 0.15682534735275272
but i want to display only 0.15,  only 2 or 3 decimal points.


can i do it,if so how?

Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

String d = new DecimalFormat("0.00").format(0.15682534735275272);
ASKER CERTIFIED SOLUTION
shji1

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
TimYates

Or do it the right way like CEHJ said ;-) *giggle*
rajuvegesna

ASKER
good,but i couldnt able to keep in my prog..i am getting error,
because i want this string to be in the middle of a class file,like

String d = new DecimalFormat("0.00").format(p.y);

where p.y varies ...

what is the DecimalFormat actually....
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
TimYates

Soz....

looked a bit closer at your code, and you are right :-/

That will perform rounding to a certain number of d.p. and is quite cool :-)

*grovel* *apologise* :-(
TimYates

So "String d = new DecimalFormat("0.00").format(p.y);" doesn't work?

what's the error?
rajuvegesna

ASKER
good,but i couldnt able to keep in my prog..i am getting error,
because i want this string to be in the middle of a class file,like

String d = new DecimalFormat("0.00").format(p.y);

where p.y varies ...

what is the DecimalFormat actually....
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
rajuvegesna

ASKER
hai Shaji ,

ur idea is good and simple..

thank u for all...
CEHJ

String d = new java.text.DecimalFormat("0.00").format(p.y);

Why would you want a number that is nearer to 0.16 to be displayed as 0.15?