473,763 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Round a float...

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
Nov 22 '05 #1
3 34923

"Terje Barman" <terje_nospam_@ _nospam_barman-hanssen.no> wrote in message
news:7N******** ************@te lenor.com...
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?


Math.round();

- Oliver
Nov 22 '05 #2
Thanks Oliver!

I found that Math.Round(floa t) -> int, thus I can do as follows:

float f = 1.234;
f *= 100;
f = (float)Math.rou nd(f)/100;
.... and now f==1.23

Terje
"Oliver Wong" <ow***@castorte ch.com> skrev i melding
news:Wp2ff.2001 72$ir4.172405@e dtnps90...

"Terje Barman" <terje_nospam_@ _nospam_barman-hanssen.no> wrote in message
news:7N******** ************@te lenor.com...
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?


Math.round();

- Oliver

Nov 22 '05 #3

"Terje Barman" <terje_nospam_@ _nospam_barman-hanssen.no> wrote in message
news:H9******** ************@te lenor.com...
Thanks Oliver!

I found that Math.Round(floa t) -> int, thus I can do as follows:

float f = 1.234;
f *= 100;
f = (float)Math.rou nd(f)/100;
... and now f==1.23

Terje


Yes, exactly. =) Good luck with your project.

- Oliver
Nov 22 '05 #4

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

Similar topics

4
10052
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 proper syntax for this? I tried Total = round(Total ) i also tried that with float. Jason...
5
12617
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
18783
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... double somenumber = round(2453126.7227083333 * 100000.0) / 100000.0; desired: somenumber =...
6
11358
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 taking the floor of that and checking if its 5 or higher, but that seems really lame. There has to...
6
7098
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
1563
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
116
35966
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 haywire. So my question is, given this code: int main() { float f = 59.89F;
3
2563
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
1573
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
9386
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.