LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-08-2020, 06:46 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
Ceiling of float number - How?


Hello All

How do I get the ceiling of the float number? I don't want float to be rounded to nearest.

For example, 11.2, 11.5 11.8 should round to its ceiling 12

I don't want traditional rounding like 11.2 = 11 and 11.8 = 12

I searched the web, but all I get is how to use a modulus trick between two numbers and finding its ceiling.

But I don't have two numbers, I only have a single float number which I derived from other complex maths. Now I want its ceiling.

The printf gives traditional rounding

Code:
$ val1=11.2
$ val2=11.8

$ printf "%.f\n" $val1
11

$ printf "%.f\n" $val2
12
For both values I want the result to be 12.

How do I do that?

Thanks
 
Old 08-08-2020, 06:56 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
A bit convoluted perhaps, but should work:
Code:
echo $(($(printf "%.0f" $f)+1))
I.e. remove the fraction, then add 1.

Last edited by berndbausch; 08-08-2020 at 06:56 AM. Reason: typo
 
Old 08-08-2020, 07:18 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545

You haven't given parameters as to what software you can/can't use - e.g. python and perl can both do it.

Alternatively, here's a hacky workaround that truncates zero decimals and increments any non-zero decimal part:
Code:
echo "$num" | sed -e 's/\.0*$//;s/\.[0-9]*$/ + 1/' | bc
Or if bc isn't available:
Code:
echo $(( echo $( echo "$num" | sed -e 's/\.0*$//;s/\.[0-9]*$/ + 1/' ) ))

But if you're dealing with floating point numbers, you should aim to be using a language that has native support for them.


Last edited by boughtonp; 08-08-2020 at 07:19 AM.
 
Old 08-08-2020, 08:12 AM   #4
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by berndbausch View Post
A bit convoluted perhaps, but should work:
Code:
echo $(($(printf "%.0f" $f)+1))
I.e. remove the fraction, then add 1.
Interesting. But what if I get a float with .00 as a decimal? Your solution made me think.

Code:
$ val1=11.00
$ val2=11.28
$ val3=11.68

$ echo $(($(printf "%.0f" $val1)+1))
12

$ echo $(($(printf "%.0f" $val2)+1))
12

$ echo $(($(printf "%.0f" $val3)+1))
13
It nuked for 11.00 and 11.68. Probably I should try with variable substring removal method something like this ${val3%.*} and add 1 to it.

But this also nuked for 11.00. Hmmm
 
Old 08-08-2020, 08:14 AM   #5
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by boughtonp View Post
You haven't given parameters as to what software you can/can't use - e.g. python and perl can both do it.

Alternatively, here's a hacky workaround that truncates zero decimals and increments any non-zero decimal part:
Code:
echo "$num" | sed -e 's/\.0*$//;s/\.[0-9]*$/ + 1/' | bc
Or if bc isn't available:
Code:
echo $(( echo $( echo "$num" | sed -e 's/\.0*$//;s/\.[0-9]*$/ + 1/' ) ))

But if you're dealing with floating point numbers, you should aim to be using a language that has native support for them.

I'm writing a bash shell script and I can use bc. I don't think using bc will create a problem.

OK this one worked

Code:
$ echo "$val1" | sed -e 's/\.0*$//;s/\.[0-9]*$/ + 1/' | bc
11

$ echo "$val2" | sed -e 's/\.0*$//;s/\.[0-9]*$/ + 1/' | bc
12

$ echo "$val3" | sed -e 's/\.0*$//;s/\.[0-9]*$/ + 1/' | bc
12

Last edited by ddenial; 08-08-2020 at 08:16 AM.
 
Old 08-08-2020, 08:29 AM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
There’s a bc solution here:

https://unix.stackexchange.com/a/168481
 
Old 08-08-2020, 09:29 AM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
If we leave out the 11.00 case, perhaps the easiest method is
Code:
echo $((${val3/.*/}+1))
 
1 members found this post helpful.
Old 08-09-2020, 05:13 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Treat the number as a string, cut it at the decimal point, look at the latter value only to decide whether you need to add 1 to the former or not.

PS: do you want 11.00001 to also be rounded up to 12?
 
2 members found this post helpful.
  


Reply



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
Hard float/soft float interndan Slackware - ARM 5 08-01-2014 10:23 AM
[SOLVED] gcc 4.6.3 'invalid operands to binary % ( have 'float' and 'float' ) error curious95 Programming 6 02-22-2013 08:51 AM
How to use a float/double without using float/double keyword? geewhan Linux - Kernel 4 06-17-2012 08:19 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:56 PM.

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
Open Source Consulting | Domain Registration