Author: Lou Adler
Format Float with Comma
Answer:
1 2 function FormatNum(Value: Extended; Decimal: Integer): string;
3 var4 SLen, SPos: Integer;
5 SVal: string;
6 begin7 Str(Value: 0: Decimal, SVal);
8 SLen := Length(SVal);
9 if Decimal = 0 then10 SPos := SLen - 2
11 else12 SPos := SLen - (Decimal + 3);
13 while SPos > 1 do14 begin15 Insert(',', SVal, SPos);
16 SPos := SPos - 3;
17 end;
18 Result := SVal;
19 end;
20 21 //Also, you can simply do this:22 23 i: Extended;
24 s: string;
25 26 i := 1000.123456;
27 s := Format('%.2n', [i]);
The value of s will be 1,000.12
You can also add your own characters, so you could do something like this:
s := Format('$%.2n', [i]);
This would output $1,000.12
So, good luck in your number to string formatting.