473,574 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to round float value?

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
Nov 17 '05 #1
2 1555
Steve wrote:
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


You should use double, it's more precise:

#include <math.h>
....
double f = 123.456789;
double r1 = floor(f * 100) / 100;
double r2 = floor(f * 100 + 0.5) / 100;

r1 is 123.45, which is 123.456 "floored" to two decimal digits.
r2 is 123.46, which is 123.456 rounded to two decimal digits.

If you really need to work with floats use floorf().
Nov 17 '05 #2
Steve wrote:
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


You should use double, it's more precise:

#include <math.h>
....
double f = 123.456789;
double r1 = floor(f * 100) / 100;
double r2 = floor(f * 100 + 0.5) / 100;

r1 is 123.45, which is 123.456 "floored" to two decimal digits.
r2 is 123.46, which is 123.456 rounded to two decimal digits.

If you really need to work with floats use floorf().
Nov 17 '05 #3

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

Similar topics

4
6778
by: tertius | last post by:
Hi, I'm trying to round my float total to the nearest .05 cents. 12.01 should produce 12.00 0.14 should produce 0.10 2.28 " 2.25 703.81 " 703.80 "%.02f"%100.0099 produces 100.01 (which I know is right)
0
2525
by: dregier | last post by:
attached is my full query that I am currently working on: Declare @counterday1 int Declare @counterday2 int set @counterday1 = '20' set @counterday2 = '629' Declare @CounterMin07_01 float Declare @CounterMin08_01 float Declare @CounterMin09_01 float
6
7091
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
7
4772
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single precision floating point types. The number of decimal digits guaranteed to be correct on my implementation is 6. (i.e numeric_limits < float >::digits10 =...
5
6810
by: shapper | last post by:
Hello, I have the following: int a; int b; var c c = a / (float)b; I used float to not get an int value as result. I think in C# I must. But know I want to round c to something like 3.48 or 7.50, ...
0
7815
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
7738
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
8077
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. ...
0
8257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7828
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...
1
5634
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
3765
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2253
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
0
1076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.