round up to nearest number and significant figures

S

Steve

1. Is there a way to convert a variable .09999999 to .1?

2. Likewise how can I convert 108229.99 to 108230.0 (it must alway be
rounded up and be an integer)

Thanks
 
T

Thomas Schodt

Steve said:
1. Is there a way to convert a variable .09999999 to .1?

For a given precision .09999999 is equal to .1
A floating point value is not stored like that.

IIRC Roedy posted a link to an explanation of floating point only a few
threads up.

Maybe you want BigDecimal?
Read what Roedy has to say.
2. Likewise how can I convert 108229.99 to 108230.0 (it must alway be
rounded up and be an integer)

double foo = 108229.99;
int bar = (int)(foo+.5);
 
F

Fred

Use the DecimalFormat class - for example here is a snippet...

import java.text.*;

class test
{
public static void main(String Args[])
{


DecimalFormat fmtObj = new DecimalFormat("####0.00");

double d1 = 1.0199999;

System.out.println(d1);
System.out.println(fmtObj.format(d1));
}

}


Here are the results from running this class file:

1.0199999
1.02
Press any key to continue...


Hope this helps you,

Fred.
 
S

Steve

Thank you, I read your web page.

I am just a bit confused how to use this information. The Math funtionality
is not recognised and I have almost no experience in java.

I presently have
float MPoriginalFormat = (MP2+MDC)*1000; //110060, could show as 110059.99

// need to convert this to 110060

Use long Math. round ( double )

Thank you.
 
S

Steve

got it, thanks
MPoriginalFormat2= (int) Math.ceil(MPoriginalFormat); //it needs to be an
integer rounded up


Steve said:
Thank you, I read your web page.

I am just a bit confused how to use this information. The Math funtionality
is not recognised and I have almost no experience in java.

I presently have
float MPoriginalFormat = (MP2+MDC)*1000; //110060, could show as 110059.99

// need to convert this to 110060

Use long Math. round ( double )

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top