Recent

Author Topic: Dumb question of the day for 'format'  (Read 3361 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Dumb question of the day for 'format'
« on: March 24, 2011, 08:38:30 pm »
Ok, I've been bouncing around the net trying to find out a way do do something with format but either it can't be done, unlikely, or I've missed something simple, probable  :-[

I have a double that I need to format to a string and have it padded with zeros.  My field needs to be '0000000.00' so if the number is 1.2 then the string should be '0000001.30' or if the number is zero the string should be '0000000.00'.  Should be simple, I thought, but nothing is making any sense here at the end of the day.   %)

Bill

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Dumb question of the day for 'format'
« Reply #1 on: March 24, 2011, 08:50:37 pm »
FPC has special function for this:

Code: [Select]
var a: Double;
begin
a:=1;
floattostrf(a, ffFixed, 0, 2);

will return 1.00
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2673
Re: Dumb question of the day for 'format'
« Reply #2 on: March 24, 2011, 08:58:03 pm »
hmmm... I would have expected that Format('%8.2f', [value]); would have done it, but according to the docs it adds only spaces.
Use FormatFloat('0000000.00', value) instead:

@Blaazen: it doesn't prefix
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

 

TinyPortal © 2005-2018