473,516 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16779
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
10045
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 SaleID and another one named Total. I am trying to take total and convert it to a float with 2 decimals top be used as money obviously. What would be the...
5
12598
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 sysusers where name = 'public'; =========== -8.88178419
8
18739
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 instead of _about_ what I want. Taken down to just the part relevant to my question, here's the code, the desired result, and the actual result... ...
6
11334
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 find the part after the decimal, but there doesn't seem to be a way to do that either. Other then subtracting the floor and multiplying by 10 and...
6
7088
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 = Math.Round( original, 2 ); But I'm not happy with the results. E.g. if original is
2
1550
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
34907
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
35683
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going...
3
2552
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 more clear. t = And I will have the next list:
2
1556
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
7276
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7548
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5110
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.