Pad Number with Leading Zeros

------_=_NextPart_001_01C146C3.16F952D0
Content-Type: text/plain;
	charset="iso-8859-1"

Hello.
 
Is there a way to pad a number with leading zeros with 1 command rather than
the 2 I used below?  It looks like you're supposed to be able to zero-fill
with sprintf but I couldn't figure it out.
 
#!/bin/perl -wd
 
$f = 1234;
 
$g = sprintf "%6s", $f;    
$g =~ s/ /0/g;                
 
print "$f\n";
print "$g\n";
 
 
 
~Carl

------_=_NextPart_001_01C146C3.16F952D0--
0
cschoeneman
9/26/2001 7:40:26 PM
perl.beginners 29388 articles. 4 followers. Follow

2 Replies
1360 Views

Similar Articles

[PageSpeed] 12
Get it on Google Play
Get it on Apple App Store

------_=_NextPart_001_01C146C7.025BA040
Content-Type: text/plain;
	charset="iso-8859-1"

	Change %6s to %06s and it will pad with zeros.

Wags ;)

-----Original Message-----
From: Schoeneman, Carl [mailto:cschoeneman@microgeneral.com]
Sent: Wednesday, September 26, 2001 12:40
To: Perl Beginners (E-mail)
Subject: Pad Number with Leading Zeros


Hello.
 
Is there a way to pad a number with leading zeros with 1 command rather than
the 2 I used below?  It looks like you're supposed to be able to zero-fill
with sprintf but I couldn't figure it out.
 
#!/bin/perl -wd
 
$f = 1234;
 
$g = sprintf "%6s", $f;    
$g =~ s/ /0/g;                
 
print "$f\n";
print "$g\n";
 
 
 
~Carl

------_=_NextPart_001_01C146C7.025BA040--
0
Wagner
9/26/2001 8:08:29 PM
On Wed, 26 Sep 2001, Schoeneman, Carl wrote:

> Is there a way to pad a number with leading zeros with 1 command rather than
> the 2 I used below?  It looks like you're supposed to be able to zero-fill
> with sprintf but I couldn't figure it out.

$x = 1234;
printf "%06d\n", $x

prints

001234

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
"I have to convince you, or at least snow you ..."
		-- Prof. Romas Aleliunas, CS 435


0
bmccoy
9/26/2001 8:24:35 PM
Reply:

Similar Artilces:

How to pad string with leading zeroes
*** please ignore the post entitled 'Brian'. Its a duplicate of *** this post without a matching title Hi, I have a requirement to convert a 3-digit integer to a 3 character string padded with leading zeroes as I SELECT it from the database. Examples: ========= 1 converts to '001' 21 converts to '021' 321 converts to '321' I have played with several string functions to no avail. Any help/suggestions would be appreciated. Brian Two possible ways to get you going: select substring(convert(char(4),column + 1000),2,3) OR ...

array of numbers with padded zeroes
I'm having trouble with this, and I'm sure someone out there has a really clever solution-- I have a hash, %data, with, say, 32 elements. From that, I need an array going from 01 to 32, i.e. 01, 02, 03, 04, etc. But if my hash had 750 elements, I'd want it to go from 001 to 750. I thought this should be easy, but I'm really struggling with it-- This is the best I've come up with: @allkeys = ('0' x (length(keys(%data)) - 1) . '1' .. keys(%data)); .... but it doesn't work. Any ideas? TIA. - Bryan Bryan R Harris wrot...

[Patch] to format for Zero Padded Numbers
--simple boundary Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Description: "cc:Mail Note Part" Since my original posting generated deafening silence, here are patches to enable zero padded numerics in Perl format lines. Context diffs to patch form.h, pp_ctl.c, t/op/write.t and pod/perlform.pod attached. Extracted from patched perlform.pod: ------------ As an alternate form of right justification, you may also use "#" or "0#" characters (with an optional ".") to specify a numeric field. This way you ...

format number with leading non-zero
Hello, I want to format a number to become a 9 digits representation, every= time it must have an integer 1 at the front.=0A=0AE.g =0A123 become 100000= 123=0A123456 become 100123456=0A=0AIf I use sprintf I can only make it to h= ave leading zero such as sprintf("%09d", $number);=0A=0AThanks.=0A=0AWillia= m=0A=0A=0ASend instant messages to your online friends http://uk.messenger.= yahoo.com Just insert the literal digit in the format and trim the size accordingly. sprintf("1%08d", $number); Bob McConnell -----Original Message----- From: William [mailto:e...

Formatting numbers without leading zeros
Hello everyone. I have problems with formatting number-columns in a datawindow. I want to display the value 12.3456 as an area using the format "#,##0.00 m�". I expect it to be "12,35 m�". But it is displaid as "0.012,35 m�", that is with leading zeros. Replacing "m�" by any other string always displays the number with leading zeros. Except for signs of currencies, as dollar ($) and euro (?). Using these signs with the same format ("#,##0.00 $") displays the number as "12,35 $" (which I would prefer). Why is this so? How...

Padding Numeric field with leading Zeros
Hi, I have a situation where I need to convert an Amount field into a CHAR field and pad it with leading Zeros, so that the total number of places occupied are 12 characters. for example : If the amount is $4.50 Then it should be converted as 000000000450 Has anyone done this in the past. If yes, could you please send me the methodology. Thanks. Regards, Murali. declare @VALUE int declare @i int select @i = 9 -- number of characters desired select @VALUE = -12345 /* leading sign */ SELECT CASE WHEN @VALUE >= 0 THEN "+" + r...

Formating number without leading zeros
Hello everyone. I am using PB 9.0.1 Build 7275. I have problems with formatting number-columns in a datawindow. I want to display the value 12.3456 as an area using the format "#,##0.00 m�". I expect it to be "12,35 m�". But it is displaid as "0.012,35 m�". That is, with leading zeros. Replacing "m�" by any other string always displays the number with leading zeros. Except for the euro (?) or dollar ($). Using these signs with the same format ("#,##0.00 ?") displays the number as "12,35 ?" (which I would prefer). Why is t...

Padding Numeric field with leading Zeros #2
Hi, I have a situation where I need to convert an Amount field into a CHAR field and pad it with leading Zeros, so that the total number of places occupied are 12 characters. for example : If the amount is $4.50 Then it should be converted as 000000000450 Has anyone done this in the past. If yes, could you please send me the methodology. Thanks. Regards, Murali. ...

Proposed change to format for Zero Padded Numbers
I needed to use zero padded numbers for a specific project, and got a little tired of writing sprintf("%010f", $field) for dozens of fields. I wanted to just use format OLD_STYLE = @>>>>>>@####.## $integer, $real . but have the output padded on the left with zeros instead of spaces. It got so bad, that I actually looked at the Perl source(!) to see if I could do it myself. To make a long story short, I made it work, but I wanted to discuss the ramifications before I send in a proposed patch. I made the following change: ...

Re:[Patch] to format for Zero Padded Numbers
--simple boundary Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Description: "cc:Mail Note Part" Oops, new copy of t/op/write.t patch; I forgot to change the number of tests at the top of the file. John Peacock --simple boundary Content-Type: text/plain; charset=US-ASCII; name="write.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="write.patch" *** t/op/write.orig Wed Feb 9 19:51:55 2000 --- t/op/write.t Fri Apr 7 13:27:39 2000 *************** *** 1,6 **** #!....

DetailsView not displaying leading zero of telephone numbers.
I have a DetailsView bound to a CustomerRecord table which displays customer name, email address and telephone number. I have the telephone numbers stored as data type varchar(20) in my SQL Server database, so the table in SQL Server 2005 shows the full telephone number with the leading zero, e.g. 01234567890. The problem is that the DetailsView does not display this like the table does. When i run the web application, the telephone numbers are displayed without the leading zero, i.e. 1234567890 instead of 01234567890. I am developing this in Visual Studio 2005 with C#. Is there any obvi...

GridView & FormView dropping leading zeros from numbers set as strings
I'm storing in a SQL database a certain field (an employee number) as a varchar data type. Throughout the app (datasource, update/insert stored procedures, etc.) I'm working with it as a string. I've even ensured that it's being used as a string "behind the scenes" by watching it in SQL Profiler. Problem is, the GridView & FormView that display it always drop any leading zeros. Wierder yet, when I put the GridView in Edit mode & add a zero, it saves to the database with the zero, but the leading zero doesn't display in the GridView! Anyone kn...

Autocomplete dropping leading zeros and trailing zeros after a hyphen
Autocomplete Web Service Code 1    Imports System.Web2    Imports System.Web.Services3    Imports System.Web.Services.Protocols4    Imports System.Data.SqlClient5    Imports System.Data6    7    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.8    <System.Web.Script.Services.ScriptService()> _9    <WebService(Namespace:="http://tempuri.org/")> _10   <WebServiceBindi...

problem with number/number=number ?
hi i need for below 1/36 = 0,0277777777777778 but result should be 1 3/36 should be 1 35/36 should be 1..... (between 1 and 36)/36 should be 1 36/36  and 37/36 and 45/36 and 71/36 should be 2  (between 72 and 37)/36 should be 2  (between 107 and 73)/36 should be 3  and go on how can i do it ? ThanksMark as me if my question or my answer can be helpful for you :) Hi, I think that (int)(n / 36) + 1 will do the trick  hi it work true without below  result 1 for 1/36 and 4 /36 and 35 /36 .. it is true.. but result 2 for 36/36 . res...

Web resources about - Pad Number with Leading Zeros - perl.beginners

Leading - Wikipedia, the free encyclopedia
... here. For the city and metropolitan borough in West Yorkshire, England, see Leeds . For other uses, see Lead (disambiguation) . "Line leading" ...

Buckeye Nation? Ohio State Dominates Oregon in Facebook Conversation Leading Up to Title Game
While the University of Oregon is favored over Ohio State University in Monday night’s College Football Playoff National Championship Game Presented ...

Hürriyet Daily News - LEADING NEWS SOURCE FOR TURKEY AND THE REGION
Hurriyet Daily News is the leading news source for Turkey and the region. Hurriyet daily news, turkish daily news, turkish news, hürriyet daily ...

A World Map of Leading Social Networks
Facebook’s international growth continues at a torrid pace in many countries around the world. Now, Vincenzo Cosenza – translator of Inside Facebook’s ...

Tehran Times - Iran's Leading International Daily
Tehran Times - Iran's Leading International Daily

CNN Leading Women (@CNNIwomen) on Twitter
Log in Sign up You are on Twitter Mobile because you are using an old version of Internet Explorer. Learn more here CNN Leading Women @ CNNIwomen ...

Virtual-Strategy Magazine - Your leading source for virtualization news
Virtual Strategy Magazine is an online publication devoted entirely to virtualization technologies.

Daily Times - Leading News Resource of Pakistan
DailyTimes delivers the latest breaking news and information on the latest top stories, entertainment, business, cricket, politics, and more. ...

Mobile News Online - The UK's leading mobile communication industry news site
This month, thousands of students will be starting university for the first time. Some will need new phones. Mystery Caller is on the case to ...

Who are the UK’s Leading Social Business Influencers?
We have attempted to create a list of the top Social Business Influencers in and around the UK. These influencers have, through what they have ...

Resources last updated: 12/20/2015 11:41:32 AM