LinuxQuestions.org
Help answer threads with 0 replies.
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-05-2009, 04: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-05-2009, 11:44 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

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


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

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

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