LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-25-2020, 12:29 AM   #1
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Stretch
Posts: 985

Rep: Reputation: 81
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
 
Old 05-25-2020, 12:34 AM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 4,833

Rep: Reputation: 1764Reputation: 1764Reputation: 1764Reputation: 1764Reputation: 1764Reputation: 1764Reputation: 1764Reputation: 1764Reputation: 1764Reputation: 1764Reputation: 1764
Divide SomeVar by 1000.
Code:
someVar/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.
Old 05-25-2020, 12:43 AM   #3
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Stretch
Posts: 985

Original Poster
Rep: Reputation: 81
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.
 
Old 05-25-2020, 12:46 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,392

Rep: Reputation: 624Reputation: 624Reputation: 624Reputation: 624Reputation: 624Reputation: 624
But in post #1 you want
901 => 0.9
900 => 0.8
?

Last edited by MadeInGermany; 05-25-2020 at 12:49 AM.
 
Old 05-25-2020, 12:55 AM   #5
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Stretch
Posts: 985

Original Poster
Rep: Reputation: 81
Quote:
Originally Posted by MadeInGermany View Post
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.
 
Old 05-25-2020, 12:56 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,890

Rep: Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207
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.
 
Old 05-25-2020, 12:57 AM   #7
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,828
Blog Entries: 3

Rep: Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398Reputation: 2398
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()
 
Old 05-25-2020, 01:07 AM   #8
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Stretch
Posts: 985

Original Poster
Rep: Reputation: 81
Quote:
Originally Posted by Turbocapitalist View Post
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.
 
Old 05-25-2020, 01:19 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,623

Rep: Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756
probably:
Code:
result = int(number / 100 ) / 10
 
1 members found this post helpful.
Old 05-25-2020, 01:30 AM   #10
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Stretch
Posts: 985

Original Poster
Rep: Reputation: 81
Quote:
Originally Posted by pan64 View Post
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.
 
Old 05-25-2020, 01:41 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,623

Rep: Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756
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 )
 
Old 05-25-2020, 02:04 AM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,407
Blog Entries: 11

Rep: Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359
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.
Old 05-25-2020, 02:19 AM   #13
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Stretch
Posts: 985

Original Poster
Rep: Reputation: 81
Quote:
Originally Posted by astrogeek View Post
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?
 
Old 05-25-2020, 02:24 AM   #14
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,407
Blog Entries: 11

Rep: Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359
Quote:
Originally Posted by lucmove View Post
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?
 
Old 05-25-2020, 03:30 AM   #15
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,392

Rep: Reputation: 624Reputation: 624Reputation: 624Reputation: 624Reputation: 624Reputation: 624
Quote:
Originally Posted by pan64 View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Assitence wiht rounding to nearest inch Timberwolf753 Programming 6 10-12-2006 06:14 PM
umask taking the nearest even number anjanesh Linux - General 4 07-17-2006 12:12 PM
PS2 mouse goes crazy [it goes round n round n round...] goci Linux - Hardware 2 10-09-2003 08:15 AM
STUPID question on routing and my nearest computer kngharv Linux - Networking 1 07-12-2002 05:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration