LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-03-2020, 05:54 PM   #1
viriv
LQ Newbie
 
Registered: Aug 2020
Posts: 3

Rep: Reputation: Disabled
printf not rounding up with %0.f on bash shell (endeavouros)


so i wanted to use light for my backlight and use xob to make a nice bar that displays it, but light outputs a number with a comma so i thought i could use printf to round it up but it just outputs a error saying (for example) 1.55 is a invalid number instead of 2, does anyone know why this might be caused?
 
Old 08-03-2020, 07:24 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
bash is integer only if you need floats use bc
 
Old 08-03-2020, 08:10 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Works ok if using a bash variable - how are you passing the value to printf ?.
 
Old 08-04-2020, 03:37 AM   #4
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,157

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
try:

printf (%0.f,variable)

In C that would print by default the 6 decimal places since you specified floating point.


printf (%0.2f,variable) <-- this would print only 2 decimal places


Or checkout this link at page 5: https://www.cypress.com/file/54441/download

But if you never give the whole code, then we can keep on assuming and guessing.

Good luck!!
 
Old 08-04-2020, 05:38 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
in shell without ( ):
Code:
printf "%0.2f" "$variable"
 
Old 08-04-2020, 09:46 AM   #6
viriv
LQ Newbie
 
Registered: Aug 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
i figured it out

i made the mistake of using dots instead of comma's wich bash gave a error for
 
Old 08-04-2020, 11:51 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by viriv View Post
i made the mistake of using dots instead of comma's wich bash gave a error for
Code:
$ LC_NUMERIC=de_DE printf %.2f\\n 1.44
bash: printf: 1.44: invalid number
0,00
$ LC_ALL=C printf %.2f\\n 1.44
1.44
 
Old 08-04-2020, 11:53 AM   #8
viriv
LQ Newbie
 
Registered: Aug 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
i found that out the hard way sigh, ddg you have failed me
 
  


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
LXer: EndeavourOS Aims to Fill the Void Left by Antergos in Arch Linux World LXer Syndicated Linux News 0 08-11-2019 02:51 PM
LXer: Ubuntu Ex86s 32-Bit, OpenMandriva, Alpine, openSUSE, EndeavourOS, Regolith | This Week in Linux 71 LXer Syndicated Linux News 0 06-29-2019 09:31 PM
[SOLVED] printf $"Hello $var\n" vs. printf "Hello $var\n" -- not a typo. What is it? GrapefruiTgirl Programming 2 10-21-2010 08:21 AM
[SOLVED] Bash rounding up numbers with printf gary_in_springhill Programming 5 02-03-2010 01:34 AM
How is 'man 3 printf' different from 'man printf' ?? purpleburple Linux - General 3 09-23-2002 12:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:04 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