473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 198906
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.

Similar topics

1
by: Gizmo | last post by:
Hi there i was wondering if any one new to a function that rounds up a float to so many decimal places. I have a number in bytes and converting it to mb's and gb's but once its converted i need to...
6
by: John Bentley | last post by:
John Bentley writes at this level: If we think about our savings accounts then division never comes in (as far as I can see). We deposit and withdraw exact amounts most of the time. Occasionaly...
4
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
13
by: kennethlou | last post by:
Hi, If in C a variable appears like X=10.000000, I can round it to zero decimal places. ie X=10? I then have to save the number into a variable. The method appears below:...
4
by: Fuzzydave | last post by:
I have been using a round command in a few places to round a value to zero decimal places using the following format, round('+value+', 0) but this consistantly returns the rounded result of...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.