473,796 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to round a floating point to nearest 10?

I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
How can I achieve that?
Aug 9 '08 #1
7 10603
On Aug 9, 9:31 pm, Will Rocisky <geekm...@gmail .comwrote:
I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
How can I achieve that?
>>import decimal
[decimal.Decimal (int(round(x, -1))) for x in (76.1, 74.9)]
[Decimal("80"), Decimal("70")]
>>>

Aug 9 '08 #2
Will Rocisky wrote:
I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
How can I achieve that?
>>help(round)
Help on built-in function round in module __builtin__:

round(...)
round(number[, ndigits]) -floating point number

Round a number to a given precision in decimal digits (default 0 digits).
This always returns a floating point number. Precision may be negative.

>>for f in 74.9, 75.0, 75.1:
.... print "%r --%r" % (f, round(f, -1))
....
74.900000000000 006 --70.0
75.0 --80.0
75.099999999999 994 --80.0

Peter
Aug 9 '08 #3
On Aug 9, 5:46*pm, Peter Otten <__pete...@web. dewrote:
Will Rocisky wrote:
I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
How can I achieve that?
>help(round)

Help on built-in function round in module __builtin__:

round(...)
* * round(number[, ndigits]) -floating point number

* * Round a number to a given precision in decimal digits (default 0 digits).
* * This always returns a floating point number. *Precision may be negative.
>for f in 74.9, 75.0, 75.1:

... * * print "%r --%r" % (f, round(f, -1))
...
74.900000000000 006 --70.0
75.0 --80.0
75.099999999999 994 --80.0

Peter
thankssss
Aug 9 '08 #4
On Sat, 9 Aug 2008 04:31:38 -0700 (PDT), Will Rocisky <ge******@gmail .com>
wrote:
>I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
How can I achieve that?
>>for n in (74.9, 76.1):
print int((n+5)/10)*10

70
80

Dan
Aug 9 '08 #5
On Aug 9, 6:31�am, Will Rocisky <geekm...@gmail .comwrote:
I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
How can I achieve that?
>>print '%.0e' % 74.9
7e+01
>>print '%.0e' % 76.1
8e+01
Aug 9 '08 #6
On Aug 10, 1:19 am, Mensanator <mensana...@aol .comwrote:
On Aug 9, 6:31 am, Will Rocisky <geekm...@gmail .comwrote:
I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
How can I achieve that?
>print '%.0e' % 74.9
7e+01
>print '%.0e' % 76.1

8e+01
But:
>>print '%.0e' % 176.1
2e+002

Giving the Subject ("How to round a floating point to nearest 10?"),
there's a strong presumption that the OP would want the answer to be
180, not 200.
Aug 9 '08 #7
On Aug 9, 4:54*pm, John Machin <sjmac...@lexic on.netwrote:
On Aug 10, 1:19 am, Mensanator <mensana...@aol .comwrote:
On Aug 9, 6:31 am, Will Rocisky <geekm...@gmail .comwrote:
I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
How can I achieve that?
>>print '%.0e' % 74.9
7e+01
>>print '%.0e' % 76.1
8e+01

But:>>print '%.0e' % 176.1

2e+002
Which would be correct if your goal was to restrain to
1 significant digit.
>
Giving the Subject ("How to round a floating point to nearest 10?"),
there's a strong presumption that the OP would want the answer to be
180, not 200.
Well, I can't read the OP's mind and the cases I HAVE encountered
are concerned about the number of significant digits. When
laboratories
report 3 digits, all my manipulations (ppm conversion, dividing
non-detect reporting limits by 2, comparison to TACO, etc. are
required to also have exactly 3 digits of significance).
>>print '%.2e' % 0.00000123456
1.23e-006
>>print '%.2e' % 123456
1.23e+005
>>print '%.2e' % 0.123000456
1.23e-001

It all depends on what the OP actually wants. He's free to ignore
my example.
Aug 9 '08 #8

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

Similar topics

11
2532
by: Russell E. Owen | last post by:
I realize this probably a stupid question, but...is it safe to round to the nearest integer by using int(round(val))? I suspect it is fine, but wanted to be sure that weird floating point representation on some platform might make it unsafe there (i.e. get the wrong value due to the floating point value being an approximation) and if so, is there a Better Way). -- Russell
6
4061
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double). // Integer calculation (fast) int iround(const double x) { return (x>=0 ? static_cast<int>(x + 0.5) : static_cast<int>(x + 0.5) } // Floating point calculation (slow) double dround(const double x) {
14
5951
by: Nils Grimsmo | last post by:
Why did round() change in Python 2.4? $ python2.3 Python 2.3.5 (#2, Jun 19 2005, 13:28:00) on linux2 >>> round(0.0225, 3) 0.023 >>> "%.3f" % round(0.0225, 3) '0.023' >>>
10
2008
by: TomislaW | last post by:
Decimal.Round(num,0) method returns: 4 for 4.4 4 for 4.5 5 for 4.6 I need 5 for 4.5 is there any other way for rounding?
9
7386
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something different. Programming Microsoft Windows with Microsoft Visual Basic.NET "The Round method with a single argument return the whole number nearest to the argument. If the argument to Round is midway between two whole numbers,
3
24773
by: Matt | last post by:
Anybody noticed that SQL Server rounds up if the value is half way between two rounded values, but C#'s Decimal.Round(Decimal,Int32) rounds to nearest even number? >From MSDN: "When d is exactly halfway between two rounded values, the result is the rounded value that has an even digit in the far right decimal position. For example, when rounded to two decimals, the value 2.345 becomes 2.34 and the value 2.355 becomes 2.36. This process...
70
3641
by: Robert Gamble | last post by:
9899:1999 5.1.2.3 Example 4 reads: "EXAMPLE 4 Implementations employing wide registers have to take care to honor appropriate semantics. Values are independent of whether they are represented in a register or in memory. For example, an implicit spilling of a register is not permitted to alter the value. Also, an explicit store and load is required to round to the precision of the storage type. In particular, casts and assignments are...
7
4477
by: kkmigas | last post by:
Can some one explain if this can be fixed using php.ini settings ? echo "round 20.545 -".round(20.545,2)."<br>"; echo "round 20.555 -".round(20.555,2)."<br>"; echo "number_format 20.545 -".number_format(20.545, 2, ',', '.')."<br>"; echo "number_format 20.555 -".number_format(20.555, 2, ',', '.')."<br>"; PHP Version 4.3.0 / FreeBSD
0
1557
by: Edwin.Madari | last post by:
>>round(76.1, -2) 100.0 80.0 76.0 builtin function round, will work for you...... Help on built-in function round in module __builtin__: round(...) round(number) -floating point number
0
10459
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
10236
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
10182
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
10017
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...
1
7552
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
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
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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
3
2928
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.