[Solved] Format function question lazarus / 21-General ---------------------------------------------------------------------- andresayang · 2023-04-30 12:25 Hello, Is there any way to have space instead of 0 in format('%d', [IntegerVal]) function ? Another question about format: is there any way to have leading 0 when using format('%10.2f', [FloatValue]) ? (I mean directly with format function not using "ReplaceText" after) Thanks ---------------------------------------------------------------------- wp · 2023-04-30 12:47 Quote from: andresayang on April 30, 2023, 02:25:53 pmIs there any way to have space instead of 0 in format('%d', [IntegerVal]) function ? I don't understand. Spaces are used for padding by default. Quote from: andresayang on April 30, 2023, 02:25:53 pmAnother question about format: is there any way to have leading 0 when using format('%10.2f', [FloatValue]) ? (I mean directly with format function not using "ReplaceText" after) I think this is not possible by means for Format(). But FormatNumber does it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   WriteLn(FormatFloat('00000000.00', 12.3));    // ---> 00000012,30 ---------------------------------------------------------------------- andresayang · 2023-04-30 12:59 Quote from: wp on April 30, 2023, 02:47:19 pmQuote from: andresayang on April 30, 2023, 02:25:53 pmIs there any way to have space instead of 0 in format('%d', [IntegerVal]) function ? I don't understand. Spaces are used for padding by default. Quote from: andresayang on April 30, 2023, 02:25:53 pmAnother question about format: is there any way to have leading 0 when using format('%10.2f', [FloatValue]) ? (I mean directly with format function not using "ReplaceText" after) I think this is not possible by means for Format(). But FormatNumber does it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   WriteLn(FormatFloat('00000000.00', 12.3));    // ---> 00000012,30 Well, Yes i think I did not explain clearly, I did not mean for padding, but for the main value: Example i:= 0, Format('%d', ) gives '0', is there any way to have ' ' (space) instead ? Cheers ---------------------------------------------------------------------- jamie · 2023-04-30 13:03 Just use the StrToINt function after the format, if it is 0 Then yourString := ' '; ---------------------------------------------------------------------- wp · 2023-04-30 14:13 Quote from: andresayang on April 30, 2023, 02:59:09 pmExample i:= 0, Format('%d', [ i]) gives '0', is there any way to have ' ' (space) instead ? You mean: you want to display a space when the value is zero? I think that the Format function cannot do it, but FormatFloat can by means of the little-known "three-section-format string": three sections separated by a semicolon: the first section for positive, the second section for negative values, and the third section for zero Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses SysUtils;const  MASK = 'PLUS 0.00;MINUS 0.00;ZERO';begin  WriteLn(FormatFloat(MASK, 1.2345));      // --> PLUS 1.23  WriteLn(FormatFloat(MASK, -1.2345));     // --> MINUS 1.23  WriteLn(FormatFloat(MASK, 0));           // --> ZERO  ReadLn;end. So, your question regarding integers is solved by the format string '0;-0; ': Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses SysUtils;const MASK = '0;-0; ';begin  WriteLn('"', FormatFloat(MASK, 12), '"');  WriteLn('"', FormatFloat(MASK, -12), '"');  WriteLn('"', FormatFloat(MASK, 0), '"');  ReadLn;end. ---------------------------------------------------------------------- andresayang · 2023-04-30 14:55 Well, If format function can not, I'll give up ... The reason is because I do not use a simple Format('%d',[val]) function, but more: '%1.1s%10.2f%10.2f  %.1d%2.2s%4d' .....(the last string length is 109 digits), and I "need" the %4d to be 4 spaces instead of 3 spaces and 0 ... Cheers ---------------------------------------------------------------------- paweld · 2023-04-30 15:12 You can use FormatFloat and pass to Format as a stringCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$R *.res} uses  SysUtils; begin  WriteLn(Format('%1.1s%10.2f%10.2f  %.1d%2.2s%4s<', ['asd', 13.234203, 93212.83211, 12, 'xx', FormatFloat('0.0;-0.0; ', 0)]));  WriteLn(Format('%1.1s%10.2f%10.2f  %.1d%2.2s%4s<', ['asd', 13.234203, 93212.83211, 12, 'xx', FormatFloat('0.0;-0.0; ', 3.1)]));end.  Rssult:Code: [Select]a     13,23  93212,83  12xx    < a     13,23  93212,83  12xx 3,1< ---------------------------------------------------------------------- andresayang · 2023-04-30 18:40 Quote from: paweld on April 30, 2023, 05:12:30 pmYou can use FormatFloat and pass to Format as a stringCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$R *.res} uses  SysUtils; begin  WriteLn(Format('%1.1s%10.2f%10.2f  %.1d%2.2s%4s<', ['asd', 13.234203, 93212.83211, 12, 'xx', FormatFloat('0.0;-0.0; ', 0)]));  WriteLn(Format('%1.1s%10.2f%10.2f  %.1d%2.2s%4s<', ['asd', 13.234203, 93212.83211, 12, 'xx', FormatFloat('0.0;-0.0; ', 3.1)]));end.  Rssult:Code: [Select]a     13,23  93212,83  12xx    < a     13,23  93212,83  12xx 3,1< Thanks, You are a genius ! Cheers Edit: Well I did not know FormatFloat function, I was always using Format. I read the online doc and: - FormatFloat('0.0;-0.0; ', 3.1) only need to be FormatFloat('#.#', 3.1) But thanks for your help. ---------------------------------------------------------------------- paweld · 2023-05-01 07:02 Quote from: andresayangI read the online doc and: - FormatFloat('0.0;-0.0; ', 3.1) only need to be FormatFloat('#.#', 3.1)as @wp mentioned above: Quote from: wp on April 30, 2023, 04:13:54 pm... but FormatFloat can by means of the little-known "three-section-format string": three sections separated by a semicolon: the first section for positive, the second section for negative values, and the third section for zero and a link to the documentation: https://www.freepascal.org/docs- html/rtl/sysutils/formatfloat.html ---------------------------------------------------------------------- wp · 2023-05-01 08:51 Quote from: andresayang on April 30, 2023, 08:40:24 pmI read the online doc and: - FormatFloat('0.0;-0.0; ', 3.1) only need to be FormatFloat('#.#', 3.1) Almost... But you wanted to replace the zero by a space, and the '#.#' mask only results in an empty string when the number is zero: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses SysUtils;begin  WriteLn('"',FormatFloat('0;-0; ', 0),'"');   // --> " "  due to the space after the 2nd semicolon in the format mask  WriteLn('"',FormatFloat('#.#', 0),'"');      // --> ""  ReadLn;end. ---------------------------------------------------------------------- andresayang · 2023-05-01 10:31 Quote from: wp on May 01, 2023, 10:51:28 amQuote from: andresayang on April 30, 2023, 08:40:24 pmI read the online doc and: - FormatFloat('0.0;-0.0; ', 3.1) only need to be FormatFloat('#.#', 3.1) Almost... But you wanted to replace the zero by a space, and the '#.#' mask only results in an empty string when the number is zero: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses SysUtils;begin  WriteLn('"',FormatFloat('0;-0; ', 0),'"');   // --> " "  due to the space after the 2nd semicolon in the format mask  WriteLn('"',FormatFloat('#.#', 0),'"');      // --> ""  ReadLn;end. Oups, I have to verify this, as I absolutely need a space, the doc says: Quote#  is also a digit place holder. If there is a corresponding digit in the value being formatted, then it replaces the #. If not, it is removed. by a space. Cheers ---------------------------------------------------------------------- wp · 2023-05-01 10:37 Quote from: andresayang on May 01, 2023, 12:31:03 pmQuote from: wp on May 01, 2023, 10:51:28 amQuote from: andresayang on April 30, 2023, 08:40:24 pmI read the online doc and: - FormatFloat('0.0;-0.0; ', 3.1) only need to be FormatFloat('#.#', 3.1) Almost... But you wanted to replace the zero by a space, and the '#.#' mask only results in an empty string when the number is zero: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses SysUtils;begin  WriteLn('"',FormatFloat('0;-0; ', 0),'"');   // --> " "  due to the space after the 2nd semicolon in the format mask  WriteLn('"',FormatFloat('#.#', 0),'"');      // --> ""  ReadLn;end. Oups, I have to verify this, as I absolutely need a space, the doc says: Quote#  is also a digit place holder. If there is a corresponding digit in the value being formatted, then it replaces the #. If not, it is removed. by a space. Cheers Don't trust the docs. Write a little test program like I did above, and you'll see. ---------------------------------------------------------------------- andresayang · 2023-05-01 11:27 QuoteDon't trust the docs. Write a little test program like I did above, and you'll see. I just did it, and thanks a lot to point out this, as you are 100% right. Cheers