java maximum precision for float

Hello all as i remmber float maximum precision is 9 digits

but why in java its : only 5 ?

if i have :

float Val = Float.parseFloat(222.222222222);

when printing im geting : 222.22223

is there any way to keep the precision or to control it ?

say i like to see only 2 digits after the dot (.) ?

thanks

[386 byte] By [Meirya] at [2007-10-3 2:23:48]
# 1

> Hello all as i remmber float maximum precision is 9

> digits

> but why in java its : only 5 ?

> if i have :

> > float Val = Float.parseFloat(222.222222222);

>

>

> when printing im geting : 222.22223

> is there any way to keep the precision or to control

> it ?

> say i like to see only 2 digits after the dot (.) ?

to get only 2 digits after the dot.

try

float Val = ((int)(Float.parseFloat(....) * 100) / 100.0);

> thanks

PMJaina at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...
# 2
Look up java.text.DecimalFormat to display the value with the number of digits that you want.
MLRona at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...
# 3

> Hello all as i remmber float maximum precision is 9

> digits but why in java its : only 5 ?

A floating point number uses a 24 bit binary mantissa. The 24th bit

is implicit. 24 bits make up ~ 16 million different numbers. This number

is about 6/7 decimal digits and that's all a float can do for you. Note

that you can multiply this mantissa with an 8 bit exponent, so you can do

fractions or rather large numbers too, but you still have about 6/7

significant decimals.

> is there any way to keep the precision or to control it ?

> say i like to see only 2 digits after the dot (.) ?

Have a look at the DecimalFormat class. Don't use the 'multiply

by 100 and divide by it' again trick because that doesn't work.

kind regards,

Jos

JosAHa at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...
# 4

> > Hello all as i remmber float maximum precision is

> 9

> > digits but why in java its : only 5 ?

>

> A floating point number uses a 24 bit binary

> mantissa. The 24th bit

> is implicit. 24 bits make up ~ 16 million different

> numbers. This number

> is about 6/7 decimal digits and that's all a float

> can do for you. Note

> that you can multiply this mantissa with an 8 bit

> exponent, so you can do

> fractions or rather large numbers too, but you still

> have about 6/7

> significant decimals.

>

> > is there any way to keep the precision or to

> control it ?

> > say i like to see only 2 digits after the dot (.) ?

>

>

> Have a look at the DecimalFormat class. Don't

> use the 'multiply

> by 100 and divide by it' again trick because that

> doesn't work.

multiply by 100 and divide by 100 is used in apps where

the end user is shown a value in 2 decimal and the

value is further used in some calculations. if you keep

the original value which has higher precision then

there will be mismatch between the calculations.

>

> kind regards,

>

> Jos

PMJaina at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...
# 5

> > Have a look at the DecimalFormat class. Don't use the 'multiply

> > by 100 and divide by it' again trick because that doesn't work.

> Multiply by 100 and divide by 100 is used in apps where the end user

> is shown a value in 2 decimal and the value is further used in some

> calculations. if you keep the original value which has higher precision

> then there will be mismatch between the calculations.

Run this and see for yourself:for (float f= 0.0f; f <= 1.0f; f+= 0.01f)

System.out.println("100*"+f+"/100.0= "+((int)(100*f))/100.0f);

kind regards,

Jos

}

JosAHa at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...
# 6

Hello all well when i i did

//java.text.DecimalFormat dt = new java.text.DecimalFormat("####.00");

dt.setMinimumIntegerDigits(1);

dt.setMinimumFractionDigits(2);

String rr = dt.format(Val);

it worked fine , but now im trying to make it round up that is if i have

33.35 it will be 33.40 or if i have 22.226 it will be 22.23

i tryed this for example :

java.math.BigDecimal d = new java.math.BigDecimal("1115.36");

d = d.setScale(2, java.math.BigDecimal.ROUND_HALF_UP);

but the result remend the same 1115.36 and not 1115.36 as i want

what im doing wrong here ?

thanks for your help

Meirya at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...
# 7
> but the result remend the same 1115.36 and not> 1115.36 as i want > what im doing wrong here ? Is there a significant difference between 1115.36 and 1115.36? Because to me they look identical.
DrClapa at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...
# 8
> but the result remend the same 1115.36 and not> 1115.36 as i want > what im doing wrong here ? > > thanks for your helpyou got me clueless?
Redxxiva at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...
# 9
sorry sorry my mistake what i ment was if i have 1115.367i excpect it to be 1115.37 after rounding up thanks
Meirya at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...
# 10

> what i ment was

> if i have 1115.367

> i excpect it to be 1115.37 after rounding up

public static void main(String[] args) {

BigDecimal d = new java.math.BigDecimal("1115.367");

d = d.setScale(2, java.math.BigDecimal.ROUND_HALF_UP);

// I see 1115.37 printed

System.out.println(d);

// If all you want is a formatted string this does

// the job in 1.5

double dub = 1115.367;

System.out.printf("%.2f", dub);

}

See also String.format()

pbrockway2a at 2007-7-14 19:22:47 > top of Java-index,Java Essentials,New To Java...