Recent

Author Topic: Best way to pad number with zeros?  (Read 5410 times)

Rave

  • Full Member
  • ***
  • Posts: 165
Best way to pad number with zeros?
« on: March 30, 2013, 09:56:10 am »
I need a way to pad number converted to string with zeros at beginning, so at end it'll have exactly six characters (if number has lest than six characters there will be zeros at beginning). So instead of 320, resulting string will be 000320 and instead of 1, 000001. I recall doing something similar in my Turbo Pascal days (was 9, maybe 10 at the time, now I'm 23 (almost, few months missing)), but can't recall exact function or anything that would point me to it.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Best way to pad number with zeros?
« Reply #1 on: March 30, 2013, 09:59:56 am »
Off the top of my head:
Code: [Select]
zero5 = format(' %.5d', [x]);Note the dot.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Rave

  • Full Member
  • ***
  • Posts: 165
Re: Best way to pad number with zeros?
« Reply #2 on: March 30, 2013, 10:03:56 am »
Thanks, that's it!

Pappy Fiddle

  • Newbie
  • Posts: 1
Re: Best way to pad number with zeros?
« Reply #3 on: August 03, 2015, 09:51:42 pm »
Simpler:

var
  y : integr;
  p : string;

begin
  y := 123456789;
  str( y, p );
  while length( p ) < 30 do
    p := '0' + p;
  writeln( p );
end.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Best way to pad number with zeros?
« Reply #4 on: August 03, 2015, 10:04:40 pm »
Simpler:
Great addition to a 2 year old(!!) topic: 4 lines of code and 2 extra variables instead of just 1 line....
Simpler indeed  %)
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Best way to pad number with zeros?
« Reply #5 on: August 04, 2015, 09:07:45 am »
Code: [Select]
result:=strutils.Dec2Numb(12345,6,10);

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Best way to pad number with zeros?
« Reply #6 on: August 04, 2015, 12:04:02 pm »
Great addition to a 2 year old(!!) topic: 4 lines of code and 2 extra variables instead of just 1 line....
Simpler indeed  %)

Yers, and Format function of course does not use any variables and is less than 4 lines of code.

From a beginners point of view the explanation of Pappy Fiddle makes sense.
The Format function is something to get used to ....

Bart

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Best way to pad number with zeros?
« Reply #7 on: August 04, 2015, 04:13:14 pm »
From a beginners point of view the explanation of Pappy Fiddle makes sense.
The Format function is something to get used to ....
Absolutely not.
No excuse to stir up an old thread with a useless addition; although it is kind of a fashion statement these days.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018