473,382 Members | 1,425 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,382 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 198922
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?...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.