472,810 Members | 4,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,810 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 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.

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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.