Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
05-25-2020, 12:29 AM
#1
Senior Member
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432
Rep:
How do I round to the nearest hundred?
I have this code:
Code:
someFunction() {
if (someVar > 2000) {return 2;}
if (someVar > 1900) {return 1.9;}
if (someVar > 1800) {return 1.8;}
if (someVar > 1700) {return 1.7;}
if (someVar > 1600) {return 1.6;}
if (someVar > 1500) {return 1.5;}
if (someVar > 1400) {return 1.4;}
if (someVar > 1300) {return 1.3;}
if (someVar > 1200) {return 1.2;}
if (someVar > 1100) {return 1.1;}
if (someVar > 1000) {return 1;}
if (someVar > 900) {return 0.9;}
if (someVar > 800) {return 0.8;}
if (someVar > 700) {return 0.7;}
if (someVar > 600) {return 0.6;}
if (someVar > 500) {return 0.5;}
if (someVar > 400) {return 0.4;}
if (someVar > 300) {return 0.3;}
if (someVar > 200) {return 0.2;}
if (someVar > 100) {return 0.1;}
}
Of course it's dumb.
It goes up to 2,000 but "someVar" could be a lot larger than that, maybe millions or billions. What is the smart way to achieve what I want?
Please assume some kind of shell scripting or pseudo code.
TIA
05-25-2020, 12:34 AM
#2
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727
Divide SomeVar by 1000.
I believe most languages can do that...which would you like to use?
Last edited by scasey; 05-25-2020 at 12:36 AM .
1 members found this post helpful.
05-25-2020, 12:43 AM
#3
Senior Member
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432
Original Poster
Rep:
That doesn't work.
900/1000 = 0.9. OK
956/1000 = 0.956. Wrong. I need 0.9.
9560/1000 = 9.56. Wrong. I need 9.5.
Any shell language or pseudo code will do.
05-25-2020, 12:46 AM
#4
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,792
But in post #1 you want
901 => 0.9
900 => 0.8
?
Last edited by MadeInGermany; 05-25-2020 at 12:49 AM .
05-25-2020, 12:55 AM
#5
Senior Member
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432
Original Poster
Rep:
Quote:
Originally Posted by
MadeInGermany
But in post #1 you want
901 => 0.9
900 => 0.8
?
Good point. What I really want is not > (greater than) but rather >= (equal to or greater than).
Sorry.
05-25-2020, 12:56 AM
#6
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,127
The awk manual has some interesting info on rounding modes - and "interesting" results that are not always classified as errors. Well worth you spend the time reading it. Might even be some code in there methinks.
05-25-2020, 12:57 AM
#7
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
In pseudo code, generic rounding is often done like this:
Code:
result = int(number * 1000 + 0.5) / 1000
If you do not need the multiplication, leave it out. Some shells deal only with integers, however, so you might need some other language instead.
If it is a question of adding trailing zeros, then use printf()
05-25-2020, 01:07 AM
#8
Senior Member
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432
Original Poster
Rep:
Quote:
Originally Posted by
Turbocapitalist
In pseudo code, generic rounding is often done like this:
Code:
result = int(number * 1000 + 0.5) / 1000
If you do not need the multiplication, leave it out. Some shells deal only with integers, however, so you might need some other language instead.
If it is a question of adding trailing zeros, then use printf()
That doesn't work either.
Testing 900 (in Tcl):
Code:
[900 * 1000 + 0.5] / 1000
900.0005
[[900 * 1000] + 0.5] / 1000
900.0005
[900 * [1000 + 0.5]] / 1000
900.45
The right result should be 0.9.
Note: please no printf(), just math.
05-25-2020, 01:19 AM
#9
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848
probably:
Code:
result = int(number / 100 ) / 10
1 members found this post helpful.
05-25-2020, 01:30 AM
#10
Senior Member
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432
Original Poster
Rep:
Quote:
Originally Posted by
pan64
probably:
Code:
result = int(number / 100 ) / 10
That is the same as dividing by 1,000. It doesn't work.
900 / 100 / 10
0.9
OK.
945 / 100 / 10
0.945
Wrong. Should be 0.9.
1945 / 100 / 10
1.9449999999999998
Wrong. Should be 1.9. I could do with 1.49, but 1.9 would be better.
7228 / 100 / 10
7.228
Wrong. Should be 7.2. I could do with 7.22, but 7.2 would be better.
Also, note that many languages have a rounding function, but:
1.9449999999999998 = 1.94. OK.
1.9459999999999998 = 1.95. Not what I want.
05-25-2020, 01:41 AM
#11
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848
you do not understand the replies. int ( something ) is a function and has a special meaning. This function cuts all the fraction part, so the return value is always an integer, especially the one which is not greater than the input, but not less than the input - 1 . The other name of this function is truncate.
This function is implemented in almost all languages. [almost] all the rounding related functions are based on this one.
Rounding is something like: int ( something + 0.5 )
05-25-2020, 02:04 AM
#12
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
According to your expected results in the original post, I suspect this is the math you are looking for:
Code:
num = (x-(x % 100))/1000
Given x = 945
num = .9
I think the question is confused by presenting it as a
rounding problem, it is not what many would think of as rounding.
Last edited by astrogeek; 05-25-2020 at 02:10 AM .
1 members found this post helpful.
05-25-2020, 02:19 AM
#13
Senior Member
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432
Original Poster
Rep:
Quote:
Originally Posted by
astrogeek
Code:
num = (x-(x % 100))/1000
Given x = 945
num = .9
What symbol is that, x divided by 100, or x remainder 100?
Neither seems to work for me. How did you test it?
05-25-2020, 02:24 AM
#14
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Quote:
Originally Posted by
lucmove
What symbol is that, x divided by 100, or x remainder 100?
Neither seems to work for me. How did you test it?
It is x
modulo 100 (probably what you mean by
remainder ), represented by % operator in most programming languages.
I didn't test it, I just wrote the math expression. You should be able to test it in any programming language which handles floating point numbers.
How did you test it?
05-25-2020, 03:30 AM
#15
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,792
Quote:
Originally Posted by
pan64
probably:
Code:
result = int(number / 100 ) / 10
That's the solution. The int function rounds down to the next integer.
int(945 / 100) / 10
int(9.45) == 9
=> 0.9
int(1945 / 100) / 10
int(19.45) == 19
=> 1.9
All times are GMT -5. The time now is 02:57 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News