473,396 Members | 1,935 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,396 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 198927
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.