Recent

Author Topic: Formatting strings in currency format  (Read 9381 times)

Wargan

  • New Member
  • *
  • Posts: 48
    • 'This way' site
Formatting strings in currency format
« on: April 12, 2017, 01:51:58 pm »
Hello!
Tell me how I can format the line for output, in monetary format, for example:

Code: Pascal  [Select][+][-]
  1. Function GetCurrencyString (input: integer): string;
  2. Begin
  3. // e.g. Input is 10000
  4. // output must be '10 000 rub. '
  5. End;

Unfortunately, I still have not found the information about setup of Format () function with the '%f' parameter.  :-[
« Last Edit: April 13, 2017, 03:13:31 pm by Wargan »
Lazarus 1.8RC5+ FPC 3.0.4.
Windows 7 x32, RAM 3 Gb.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Formatting strings in currency format
« Reply #1 on: April 12, 2017, 02:35:30 pm »
Information about format can be found here.

But, for your specific case, it might perhaps be easier to use formatfloat function:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. // e.g. Input is 10000
  9. // output must be '10 000 rub. '
  10.  
  11. Function Int2CurrencyStr(Value: integer): string;
  12. begin
  13.   Result := FormatFloat('#,### rub', Extended(Value));
  14. end;
  15.  
  16. begin
  17.   WriteLn(Int2CurrencyStr(10000));
  18. end.
  19.  

In case you do wish to use the format function, then afaik you should be looking at %M argument, not %F. Since things are depending on (your global) formatsettings, it complicates things a little.

But, perhaps the showed example is enough for you to work with.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Formatting strings in currency format
« Reply #2 on: April 12, 2017, 02:44:06 pm »
Format() wants the code %m to add the currency string. The currency string is taken from the FormatSettings of your system. If you want another currency string use the overloaded version with the FormatSettings as last parameter:

Code: Pascal  [Select][+][-]
  1. var
  2.   val: Currency;  // or Double, or Extended
  3.   fs: TFormatSettings;
  4. begin
  5.   val := 1002.23;
  6.  
  7.   // Use currency string of current format settings
  8.   Label1.Caption := Format('%m', [val]);
  9.  
  10.   // or: Use alternate currency string
  11.   fs := DefaultFormatSettings;
  12.   fs.CurrencyString := 'rub';
  13.   Label1.Caption := Format('%m', [val], fs);

See http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/format.html

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Formatting strings in currency format
« Reply #3 on: April 12, 2017, 02:52:29 pm »
With format and %m parameter, you might also supply a TFormatSettings, so you can override the current system setting.
You can change CurrencyFormat (see: http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.TFormatSettings.CurrencyFormat) and CurrencyString


Code: [Select]
var
  FS: TFormatSettings;
  S: String;
begin
  FS := DefaultFormatSettings;
  FS.CurrencyFormat := 3; // amount followed by space and CurrencyString
  FS.CurrencyString := 'Rub';
  FS.DecimalSeparator := '.';
  FS.ThousandSeparator := #32; //a space
  S := Format('%m',[12345.67],FS);
  writeln(S);
end;

Bart

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: Formatting strings in currency format
« Reply #4 on: April 12, 2017, 10:31:12 pm »
Code: Pascal  [Select][+][-]
  1. function GetCurrencyString(Input: Integer): string;
  2. var
  3.   FS: TFormatSettings;
  4. begin
  5.   FS := DefaultFormatSettings;
  6.   FS.CurrencyString := 'rub. ';
  7.   FS.DecimalSeparator := '.';
  8.   FS.ThousandSeparator := ' ';
  9.   FmtStr(Result, '%.0m', [Currency(Input)], FS);
  10. end;

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Formatting strings in currency format
« Reply #5 on: April 13, 2017, 11:23:13 am »
Just one addition to Bart's or ASerge's solution -- the correct thousand separator for Russian shoud not be space (char(32)), but non-breaking space  or perhaps NARROW NO-BREAK SPACE. However, FPC defines ThounsandSeparator as Char and non-breaking space characters are represented with more than one byte in UTF8. For what you need, this can be worked-around with StringReplace:

So, Bart's code with this addition will become:
Code: Pascal  [Select][+][-]
  1. const
  2.   NonBreakingSpace: String = Char($C2) + Char($A0); // non-breaking space utf8-encoded
  3.   // or, maybe better:
  4.   //NonBreakingSpace: String = Char($E2) + Char($80) + Char($AF); // narrow non-breaking space
  5. var
  6.   FS: TFormatSettings;
  7.   S: String;
  8. begin
  9.   FS := DefaultFormatSettings;
  10.   FS.CurrencyFormat := 3; // amount followed by space and CurrencyString
  11.   FS.CurrencyString := 'Rub';
  12.   FS.DecimalSeparator := '.';
  13.   FS.ThousandSeparator := #32; //a space
  14.   S := Format('%m',[12345.67],FS);
  15.  
  16.   S := StringReplace(S, #32, NonBreakingSpace, [rfReplaceAll]);
  17.  
  18.   writeln(S);
  19. end;
  20.  

Wargan

  • New Member
  • *
  • Posts: 48
    • 'This way' site
Re: Formatting strings in currency format
« Reply #6 on: April 13, 2017, 03:13:12 pm »
Many thanks to all!

It really works the way.
I hope this will help in the future to the same beginners like me :)
Lazarus 1.8RC5+ FPC 3.0.4.
Windows 7 x32, RAM 3 Gb.

 

TinyPortal © 2005-2018