473,405 Members | 2,167 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,405 software developers and data experts.

How do you round off a float?

Does anyone have any sample code on how to round off a float to x
digits? say for example rounding 3.1415 to 3.1?

Any help would be appreciated. I am learning C++ and have not found
this info on the net (though it might be out there)

tiny k

Jul 22 '05 #1
9 16767
kd********@yahoo.com wrote:
Does anyone have any sample code on how to round off a float to x
digits? say for example rounding 3.1415 to 3.1?

Any help would be appreciated. I am learning C++ and have not found
this info on the net (though it might be out there)

tiny k


Integer casts will truncate to the right of the decimal point.
This suggests moving the point back and forth.

*pseudocode*

declare somenumber; // 3.1415
declare someprecision; // x, say equals 1.

shifter = 10_to_the_power_of( someprecision );
shift_it = somenumber*shifter;
truncate_it = integer_part_of( shift_it );
shift_it_back = truncate_it/shifter; // Result.

*end pseudocode*

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #2
On 1 Jan 2005 23:52:33 -0800 in comp.lang.c++, kd********@yahoo.com
wrote,
Does anyone have any sample code on how to round off a float to x
digits? say for example rounding 3.1415 to 3.1?


You do it when you format for display.
cout << setprecision(2) << pi;

Never expect an exact floating point value. Typical binary floating
point implementation cannot even represent 3.1 exactly, so forget
"rounding".

Also, prefer double over float unless you have a specific reason.

Jul 22 '05 #3

<kd********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Does anyone have any sample code on how to round off a float to x
digits? say for example rounding 3.1415 to 3.1?

Any help would be appreciated. I am learning C++ and have not found
this info on the net (though it might be out there)


o Floating point formats are almost always in binary, not in decimal.
Many decimal numbers cannot be exactly represented by the underlying
floating point format - 3.1 is an example. Rounding to a certain number of
binary digits is very different from rounding to a certain number of decimal
digits.

o Rounding of floating point values should only done with the final
result for purposes of presenting to humans. Rounding intermediate values is
a bad idea, only useful for demonstrating how badly it can trash your
results. Why this is true is not so easy to grasp, many smart people have
fallen victim to misunderstanding the need to use all the precision
available. Check out Prof. Kahan's work on this (google for "Kahan floating
point").

o A corollary to the above rule is use the maximum precision available on
your machine. For x86, that would be 80 bit long doubles. (Unfortunately,
some popular C++ compilers do not support 80 bit long doubles, they max out
at 64. Digital Mars C++ supports 80 bit long doubles.) You can get away with
lesser precision if speed and memory consumption are paramount and accurate
results are not (such as in game graphics engines).

o Rounding to a certain number of digits for display of the final result
is handilly performed by the 'precision' field in the printf format family
of functions. For example,
printf("%.8g\n", d);
prints d with 8 digits of precision. Note the decimal point before the 8.

Hope this helps!

-Walter
www.digitalmars.com free C, C++, D compilers
"code of the nerds"
Jul 22 '05 #4
It did! I forgot about printf. My code looks like this now and seems
to work just fine!

num = num + .05;
printf = ("rounded number: %4.1f", num);

Thanks for the help!

p.s. does digital mars fit on a 64 meg flash disk and does it run
without having to be installed? I am currently using djgpp but find it
is a bit less user friendly than what I would like.

Thanks
tiny k

Jul 22 '05 #5

<kd********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
It did! I forgot about printf. My code looks like this now and seems
to work just fine!

num = num + .05;
printf = ("rounded number: %4.1f", num);

Thanks for the help!

p.s. does digital mars fit on a 64 meg flash disk and does it run
without having to be installed? I am currently using djgpp but find it
is a bit less user friendly than what I would like.


Visit the digitalmars.com web site to find out these things,
which are not language issues.

-Mike
Jul 22 '05 #6
I posted a language issue. I got a reply that spurred another
question, and I don't see another way to contact the person (walter)
who is FROM digital mars and I don't see a reason why I have to find
another way to do it. If you want to start a flame war, feel free.
In the meantime my question for WALTER is still valid.
tiny k

Jul 22 '05 #7
kd********@yahoo.com wrote:
I posted a language issue. I got a reply that spurred another
question, and I don't see another way to contact the person (walter)
who is FROM digital mars and I don't see a reason why I have to find
another way to do it. If you want to start a flame war, feel free.
In the meantime my question for WALTER is still valid.
tiny k


Yo there! Relax man! Staying nice is the way to get help here. :-)

Please note: at the bottom of http://www.digitalmars.com/ is a button
that says 'send email to Digital Mars'. It's likely Walter will get it.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #8

<kd********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
It did! I forgot about printf. My code looks like this now and seems
to work just fine!

num = num + .05;
printf = ("rounded number: %4.1f", num);

Thanks for the help!
Sure! You don't need to add the .05, the printf formatter will do the "add
half and round" for you (or it should).
p.s. does digital mars fit on a 64 meg flash disk
Yes. The free download for it is 3 Mb.
and does it run without having to be installed?


Yes (though you do need to unzip it!).

-Walter
www.digitalmars.com free C, C++, D compilers

Jul 22 '05 #9
<kd********@yahoo.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I posted a language issue.
Yes, originally.
I got a reply that spurred another
question,
If you're talking about the questions about operating
the DM compiler, those are not topical here.
and I don't see another way to contact the person (walter)
who is FROM digital mars
"Don't see another way", does not make these issues topical.
Have you tried checking the DM site for support contacts?
and I don't see a reason why I have to find
another way to do it.
A very good reason is to prevent alienating yourself
from the folks here who can help you.
If you want to start a flame war, feel free.
No flame war wanted or needed.
In the meantime my question for WALTER is still valid.


It's not topical here, regardless of whether you call it
'valid' or not.

Please see:
http://www.slack.net/~shiva/welcome.txt

-Mike
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jason Tesser | last post by:
I am using Rekall which uses Python as it's scripting language and I have a question about Python. I am developing a POS system and am working on the checkout form. I pass a parameter named...
5
by: Peter Scheurer | last post by:
Hi, we found some strange behavior when operating with floats and round(). The following simplified statement reproduces the problem. select 6.56 - round(convert(float, 6.56), 2) from...
8
by: David Corby | last post by:
Hi everybody, I've got a problem. I'm trying to round a double to a particular number of significant digits, in this case 5, but I can't figure out a way around getting _exactly_ what I want...
6
by: Nobody | last post by:
How does one round a float? ie... 4.4 returns 4, while 4.5 returns 5. I see the floor and ceiling functions, but that floor would take 4.4 and return 4 and ceiling would return 5. I guess I could...
6
by: ng_mr | last post by:
No, not a question about "banker's rounding" or whatever it's called. I want to round a double to the nearest 100th, so I perform the following: // original is a double double result =...
2
by: Steve | last post by:
I have float value, let say 123.456789. How can I round it to two decimal digits after decimal point? For example: 123.456789 -> 123.45 It should be some simple way ... Thanks
3
by: Terje Barman | last post by:
Hi! I have sumarized float values and I'd like to round the sum to a certain precision. Say I have 1.234 and want it rounded to 1.23. Which method can I use? Terje
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
3
by: Nader | last post by:
Hello, I have a list of tuple with strin elements. These elements are number, but they are save as string. Now I will change the string to number which will be rounded. An example will make it...
2
by: Nimbus | last post by:
In my case, if the float value is 74.716, it has to rounding into 74.72. But is that value is 74.715, it should be 74.71.. can anyone tel me what rounding ways i need to use? Thanks in advance
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.