LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 12-10-2009, 12:01 AM   #1
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Lenght of a number?


How can I get the lenght of a number?For example if my number is 123456789 I should get the output as 9.
 
Old 12-10-2009, 12:10 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Assuming the number is a string variable (or for this purpose, make it one), the length of a string can be found using the echo ${#variable} syntax. Check the bash man page for this and many other neat string things

shell$ man bash

Sasha
 
Old 12-10-2009, 12:16 AM   #3
gaurav1086
LQ Newbie
 
Registered: Dec 2008
Location: bangalore,india
Distribution: debian lenny
Posts: 13

Rep: Reputation: 0
perl liner

Quote:
Originally Posted by ashok.g View Post
How can I get the lenght of a number?For example if my number is 123456789 I should get the output as 9.
hello . use a perl one-liner in your script as
Code:
perl -wln -e 'print length($_);

or

num=$(echo $number |  perl -wln -e 'print length($_);)
there you go
regards.
 
Old 12-10-2009, 12:31 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by ashok.g View Post
How can I get the lenght of a number?For example if my number is 123456789 I should get the output as 9.
Code:
$ number=123456789
$ length=${#number}
$ echo $length
 
Old 12-10-2009, 12:33 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by gaurav1086 View Post
hello . use a perl one-liner in your script as
Code:
perl -wln -e 'print length($_);

or

num=$(echo $number |  perl -wln -e 'print length($_);)
there you go
regards.
that's one of the many ways, but calling an external command is unnecessary and adds overhead.
 
Old 12-10-2009, 12:49 AM   #6
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by ghostdog74 View Post
Code:
$ number=123456789
$ length=${#number}
$ echo $length
Here I am getting the output as :
Code:
bash: ${#$n}: bad substitution
 
Old 12-10-2009, 12:54 AM   #7
urban.yoga.journeys
Member
 
Registered: Jun 2008
Posts: 31

Rep: Reputation: 16
Code:
mo@mo-laptop:~/python$ number=12345
mo@mo-laptop:~/python$ length=${#number}
mo@mo-laptop:~/python$ echo $length
5
should work.....
 
Old 12-10-2009, 12:59 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,516
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Quote:
Originally Posted by ashok.g View Post
Here I am getting the output as :
Code:
bash: ${#$n}: bad substitution
What were the commands you entered to get that error?
 
Old 12-10-2009, 01:00 AM   #9
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by ashok.g View Post
Here I am getting the output as :
Code:
bash: ${#$n}: bad substitution
Sorry, I used some wrong statement.

It's all working fine. I am getting the desired output.
 
Old 12-10-2009, 02:58 AM   #10
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
There is one more way to do it in C:

Go on dividing the number by 10 i.e.

If you divide 1234 by 10, u get the remainder as 4 and quotient as 123.

So what u can do is start a counter which gets incremented each time u get a quotient.

At the end the length of the original number will be counter+1 !

Last edited by TheIndependentAquarius; 12-10-2009 at 03:00 AM.
 
  


Reply

Tags
length, number


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
limite the file name lenght on samba share procfs Linux - Newbie 2 07-07-2008 10:58 PM
MAX lenght buffer in socket lrios Programming 3 05-11-2008 05:47 PM
SLES 10.0 SP1 Password Lenght Tassu Linux - Server 8 12-07-2007 12:48 AM
How to know audio file lenght? RomanG Linux - Software 1 07-23-2005 07:46 AM
mount cdrom - filenames lenght cutting drenal Linux - Newbie 3 12-27-2003 08:50 AM


All times are GMT -5. The time now is 03:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration