364,083 Members | 5987 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Rounding off a float to two decimal places

Joe M Blow
P: n/a
Joe M Blow
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
Share this Question
Share on Google+
7 Replies


Phil...
P: n/a
Phil...
x = (double)int((x+0.005)*100.0)/100.0;

"Joe M Blow" <joemblow@rogers.com> wrote in message
news:rI%gb.261229$Lnr1.5787@news01.bloor.is.net.ca ble.rogers.com...[color=blue]
> 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]


Jul 17 '05 #2

Joe M Blow
P: n/a
Joe M Blow
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... <rynes@ieee.org> wrote in message
news:YQ%gb.709974$Ho3.156706@sccrnsc03...[color=blue]
> x = (double)int((x+0.005)*100.0)/100.0;
>
> "Joe M Blow" <joemblow@rogers.com> wrote in message
> news:rI%gb.261229$Lnr1.5787@news01.bloor.is.net.ca ble.rogers.com...[color=green]
> > Hi evreyone
> >
> > I just want to kno how to make my value (a float) and round it off to[/color][/color]
two[color=blue][color=green]
> > decimal places
> >
> >
> > Thanks alot
> >
> > Ty
> >
> >[/color]
>
>[/color]


Jul 17 '05 #3

Phil...
P: n/a
Phil...
x = Math.round(x*100.0) / 100.0;


"Joe M Blow" <joemblow@rogers.com> wrote in message
news:L31hb.261954$Lnr1.142716@news01.bloor.is.net. cable.rogers.com...[color=blue]
> 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... <rynes@ieee.org> wrote in message
> news:YQ%gb.709974$Ho3.156706@sccrnsc03...[color=green]
> > x = (double)int((x+0.005)*100.0)/100.0;
> >
> > "Joe M Blow" <joemblow@rogers.com> wrote in message
> > news:rI%gb.261229$Lnr1.5787@news01.bloor.is.net.ca ble.rogers.com...[color=darkred]
> > > Hi evreyone
> > >
> > > I just want to kno how to make my value (a float) and round it off to[/color][/color]
> two[color=green][color=darkred]
> > > decimal places
> > >
> > >
> > > Thanks alot
> > >
> > > Ty
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Jul 17 '05 #4

Margaret
P: n/a
Margaret
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=blue]
> 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]
Jul 17 '05 #5

DaiIchi
P: n/a
DaiIchi
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);
Jul 17 '05 #6

John Kordyback
P: n/a
John Kordyback
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:[color=blue]
> 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]

Jul 17 '05 #7

Virendra Khanzode
P: 1
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

Post your reply

Help answer this question



Didn't find the answer to your Java question?

You can also browse similar questions: Java decimal java decimal x decimalformat 2 decimal round float