LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-05-2009, 05:26 PM   #1
lin*x
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Rep: Reputation: 1
Bash Maths... Keeping leading zero's


Hi All,

Trying to do some maths and i want to keep the leading zero's at the end of the equations....

Code:
echo $((00010-1))
9
Well this is no good, i need to use something so that i can do things like arbitrary amounts of leading zeros can all be in the answer at the end of the equation.
ie

Code:
echo $((010-1))
009
echo $((0001110-1))
0001109
echo $((00000010-1))
00000009
Etc etc, Is this possible in bash without having to use a multitude of commands and other scripts. I've read its possible in perl but so far most places on the net seem to talk about how to remove the damn leading 0's, when i want to keep them. I know i could write a long script that counts the number of '0' characters before a different number is seen and then just adding those as leading zero's after the arithmatic has taken place but there must be a simpler method?

Cheers All
 
Old 08-06-2009, 12:44 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
printf http://tldp.org/LDP/Bash-Beginners-G...#sect_06_03_06
http://en.wikipedia.org/wiki/Printf
 
  


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
Bash maths question kinetik Programming 9 01-20-2009 05:43 AM
LXer: Use Project Zero's APIs to build a simple wiki LXer Syndicated Linux News 0 01-11-2008 10:40 AM
Perl substr goes crazy when presented with zero's nodger Programming 4 01-21-2007 10:49 AM
Bash: keeping track of newly added files madtinkerer Programming 5 09-13-2006 10:30 AM
logrotate log files - keeping in order when using grep or cat and bash dmellem Linux - Software 6 02-11-2005 02:05 PM

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

All times are GMT -5. The time now is 09:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration