473,471 Members | 2,040 Online
Bytes | Software Development & Data Engineering Community
Create 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 1547
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
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 ...
0
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...
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 =...
7
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...
5
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...
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.