472,810 Members | 4,355 Online

# Rounding off a float to two decimal places

Hi evreyone

I just want to kno how to make my value (a float) and round it off to two
decimal places
Thanks alot

Ty
Jul 17 '05 #1
7 198732
x = (double)int((x+0.005)*100.0)/100.0;

"Joe M Blow" <jo******@rogers.com> wrote in message
news:rI********************@news01.bloor.is.net.ca ble.rogers.com...
Hi evreyone

I just want to kno how to make my value (a float) and round it off to two
decimal places
Thanks alot

Ty

Jul 17 '05 #2
I see what you are doing but i was looking for something more like...

Math.round(variable);
that rounds my variable to the nearest whole number...
i want it only to two decimal places

Phil... <ry***@ieee.org> wrote in message
news:YQ%gb.709974\$Ho3.156706@sccrnsc03...
x = (double)int((x+0.005)*100.0)/100.0;

"Joe M Blow" <jo******@rogers.com> wrote in message
news:rI********************@news01.bloor.is.net.ca ble.rogers.com...
Hi evreyone

I just want to kno how to make my value (a float) and round it off to two decimal places
Thanks alot

Ty

Jul 17 '05 #3
x = Math.round(x*100.0) / 100.0;
"Joe M Blow" <jo******@rogers.com> wrote in message
news:L3**********************@news01.bloor.is.net. cable.rogers.com...
I see what you are doing but i was looking for something more like...

Math.round(variable);
that rounds my variable to the nearest whole number...
i want it only to two decimal places

Phil... <ry***@ieee.org> wrote in message
news:YQ%gb.709974\$Ho3.156706@sccrnsc03...
x = (double)int((x+0.005)*100.0)/100.0;

"Joe M Blow" <jo******@rogers.com> wrote in message
news:rI********************@news01.bloor.is.net.ca ble.rogers.com...
Hi evreyone

I just want to kno how to make my value (a float) and round it off to two decimal places
Thanks alot

Ty

Jul 17 '05 #4
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
double dd = 100.2397;
double dd2dec = new Double(df2.format(dd)).doubleValue();

The value of dd2dec will be 100.24

"Joe M Blow" <jo******@rogers.com> wrote in message news:<rI********************@news01.bloor.is.net.c able.rogers.com>...
Hi evreyone

I just want to kno how to make my value (a float) and round it off to two
decimal places
Thanks alot

Ty

Jul 17 '05 #5
Ri***********@hvbamericas.com (Margaret) wrote in message news:<c1*************************@posting.google.c om>...
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
double dd = 100.2397;
double dd2dec = new Double(df2.format(dd)).doubleValue();

The value of dd2dec will be 100.24

"Joe M Blow" <jo******@rogers.com> wrote in message news:<rI********************@news01.bloor.is.net.c able.rogers.com>...
Hi evreyone

I just want to kno how to make my value (a float) and round it off to two
decimal places
Thanks alot

Ty

How about: f = (float) (Math.round(n*100.0f)/100.0f);
Jul 17 '05 #6
double r = 5.1234;
System.out.println(r); // r is 5.1234

int decimalPlaces = 2;
BigDecimal bd = new BigDecimal(r);

// setScale is immutable
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
r = bd.doubleValue();

System.out.println(r); // r is 5.12

/qb

Joe M Blow wrote:
Hi evreyone

I just want to kno how to make my value (a float) and round it off to two
decimal places
Thanks alot

Ty

Jul 17 '05 #7
How if i want 123 to be changed to 123.00 to maintain consisten precision for a good view?

Rita_Shpilsky@hvbamericas.com (Margaret) wrote in message news:<c1c3db4a.0310090942.f5e1280@posting.google.c om>...[color=blue]
> DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
> double dd = 100.2397;
> double dd2dec = new Double(df2.format(dd)).doubleValue();
>
> The value of dd2dec will be 100.24
>
>
>
> "Joe M Blow" <joemblow@rogers.com> wrote in message news:<rI%gb.261229\$Lnr1.5787@news01.bloor.is.net.c able.rogers.com>...[color=green]
> > Hi evreyone
> >
> > I just want to kno how to make my value (a float) and round it off to two
> > decimal places
> >
> >
> > Thanks alot
> >
> > Ty[/color][/color]

How about: f = (float) (Math.round(n*100.0f)/100.0f);
May 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.