From f9c7a5f62f96fab9ce5e3d2c205bc9da3046c4d4 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sun, 8 Jun 2008 17:35:32 +0200 Subject: Format numbers according to used locale --- UCoreUtils.pas | 58 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 15 deletions(-) (limited to 'UCoreUtils.pas') diff --git a/UCoreUtils.pas b/UCoreUtils.pas index f297fe0..31c72b1 100644 --- a/UCoreUtils.pas +++ b/UCoreUtils.pas @@ -57,7 +57,7 @@ function GetErrorString(ErrorNo: integer): string; function GetSignalString(SignalNo: integer): string; function UnixTimeToTDateTime(UnixTime: Int64): TDateTime; -function FormatSize(Value: Int64; Base: integer): string; +function FormatSize(Value: Int64; Base: integer; OverrideSizeFormat: integer = -1): string; function AttrToStr(const Mode: Cardinal; IncludeFileType: boolean = True): string; function AttrToOctal(const Mode: Cardinal): integer; @@ -196,14 +196,24 @@ begin Insert(Sep, Result, i); end; -function FormatSize(Value: Int64; Base: integer): string; +function FormatSize(Value: Int64; Base: integer; OverrideSizeFormat: integer = -1): string; var s: string; - i: integer; + f, i: integer; + p: PChar; + x: gdouble; begin if Base < 1 then Base := 1; - case ConfSizeFormat of + f := OverrideSizeFormat; + if f < 0 then f := ConfSizeFormat; + case f of 0 : begin // System default formatting - Result := FormatFloat('###,###,##0', Value div Base); + p := malloc(255); + memset(p, 0, 255); + if sprintf(p, '%''lu', Value div Base) < 1 then begin + DebugMsg(['FormatSize(0): sprintf() failed, using old format function.']); + Result := FormatFloat('###,###,##0', Value div Base); + end else Result := StrToUTF8(p); + Libc.free(p); end; 1 : begin // 123456 Result := IntToStr(Value div Base); @@ -218,17 +228,35 @@ begin Result := FormatFloat64(Value div Base, ''''); end; 5 : begin // 123kB - Grouped - s := '###,###,##0'; - if ConfSizeGroupPrecision > 0 then begin - s := s + '.'; - for i := 1 to ConfSizeGroupPrecision do - if ConfSizeGroupRequestZeroDigits then s := s + '0' - else s := s + '#'; + if (Value >= 1024*1024*1024) or (Base = 1024*1024*1024) then begin + x := Value / (1024*1024*1024); + p := g_strdup_printf(PChar('%''.' + IntToStr(ConfSizeGroupPrecision) + 'f GB'), x); + end else + if (Value >= 1024*1024) or (Base = 1024*1024) then begin + x := Value / (1024*1024); + p := g_strdup_printf(PChar('%''.' + IntToStr(ConfSizeGroupPrecision) + 'f MB'), x); + end else + if (Value >= 1024) or (Base = 1024) then begin + x := Value / 1024; + p := g_strdup_printf(PChar('%''.' + IntToStr(ConfSizeGroupPrecision) + 'f kB'), x); + end else p := g_strdup_printf('%d', Value); + if p = nil then begin + DebugMsg(['FormatSize(5): g_strdup_printf() failed, using old format function.']); + s := '###,###,##0'; + if ConfSizeGroupPrecision > 0 then begin + s := s + '.'; + for i := 1 to ConfSizeGroupPrecision do + if ConfSizeGroupRequestZeroDigits then s := s + '0' + else s := s + '#'; + end; + if (Value >= 1024*1024*1024) or (Base = 1024*1024*1024) then Result := FormatFloat(s + ' GB', Value / (1024*1024*1024)) else + if (Value >= 1024*1024) or (Base = 1024*1024) then Result := FormatFloat(s + ' MB', Value / (1024*1024)) else + if (Value >= 1024) or (Base = 1024) then Result := FormatFloat(s + ' kB', Value / (1024)) else + Result := IntToStr(Value); + end else begin + Result := StrToUTF8(p); + g_free(p); end; - if (Value >= 1024*1024*1024) or (Base = 1024*1024*1024) then Result := FormatFloat(s + ' GB', Value / (1024*1024*1024)) else - if (Value >= 1024*1024) or (Base = 1024*1024) then Result := FormatFloat(s + ' MB', Value / (1024*1024)) else - if (Value >= 1024) or (Base = 1024) then Result := FormatFloat(s + ' kB', Value / (1024)) else - Result := IntToStr(Value); end; end; if ConfSizeFormat in [0..4] then begin -- cgit v1.2.3