I feel embarrassed to ask this, but how to format a small decimal number without zeros to right and thousand separator (like Windows Calculator does)?

I feel embarrassed to ask this, but how to format a small decimal number without zeros to right and thousand separator (like Windows Calculator does)?

var
D1, D2, D3: Double;
begin
D1 := -1234000.9000001;
D2 := -1234000.90000001;
D3 := 1234000.9000000100000;

ShowMessage(?); //Want to display D1: '-1,234,000.9000001'
ShowMessage(?); //Want to display D2: '-1,234,000.90000001'
ShowMessage(?); //Want to display D3: '1,234,000.90000001'
end;

Where ? I tried numerous masks for FormatFloat(), FloatToStrF() and Format()

Comments

  1. I think I wasn´t clear in my post.

    Format('%0.7n', [D1], fs)
    Format('%0.7n', [D2], fs)
    Format('%0.7n', [D3], fs)

    Like you suggest will print different what I want:
    -1.234.000,9000001
    -1.234.000,9000000
    1.234.000,9000000

    I want a unique mask or rule to display D1, D2 and D3 or whatever other decimal length like my comment on code. And not fix like always 7 like you suggest.

    The closest I got was this, but without thousand separator:
    Format('%g', [Value], fs);

    ReplyDelete
  2. I guess you have to do it manually: convert to string, remove trailing zeros, and remove the trailing dot (ex. "-1234000.00000" -> "-1234000")

    ReplyDelete
  3. '0,.#########' works better than '%g'

    I limited the number of decimal places to 9 and problem solved for while... Thanks!

    ReplyDelete

Post a Comment