GetLocaleDecimal (All OS)

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
mk-soft
Beiträge: 3695
Registriert: 24.11.2004 13:12
Wohnort: Germany

GetLocaleDecimal (All OS)

Beitrag von mk-soft »

Mit LocaleStrF(...) und LocaleStrD(...)

Ist schon im englischen Forum, macht aber hier mehr sinn :wink:

Code: Alles auswählen

;-TOP

; Comment       : Function GetLocaleDecimal and GetLocalThousend
; Author        : Michael Kastner
; Second Author : 
; File          : GetLocaleFormat.pb
; Version       : 1.12
; Create        : 25.06.2018
; Update        : 30.06.2018
; 
; OS            : All
; Compilermode  : 
;

; *****************************************************************************

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  
  #LC_ALL       = 0
  #LC_COLLATE   = 1
  #LC_CTYPE     = 2
  #LC_MONETARY  = 3
  #LC_NUMERIC   = 4
  #LC_TIME      = 5
  #LC_MESSAGES  = 6
  
  ImportC ""
    setlocale(Type, *name)
    localeconv()
  EndImport
  
  ImportC ""
    snprintf(*result.Ascii, result_size.i, format.p-ascii, number.d)
  EndImport
  
  Structure lconv ; {
    *decimal_point;
    *thousands_sep;
    *grouping     ;	
    *int_curr_symbol;
    *currency_symbol;
    *mon_decimal_point;
    *mon_thousands_sep;
    *mon_grouping     ;
    *positive_sign    ;
    *negative_sign    ;
    int_frac_digits.a ;
    frac_digits.a     ;
    p_cs_precedes.a   ;
    p_sep_by_space.a  ;
    n_cs_precedes.a   ;
    n_sep_by_space.a  ;
    p_sign_posn.a     ;
    n_sign_posn.a     ;
  EndStructure        ;} lconv
CompilerEndIf

; *****************************************************************************

Procedure.s GetLocaleDecimal()
  Protected result.s
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      Protected r1.s{2}
      If GetLocaleInfo_(#LOCALE_USER_DEFAULT, #LOCALE_SDECIMAL, @r1, 2)
        result = r1
      Else
        result = "."
      EndIf
      
    CompilerCase #PB_OS_MacOS
      Protected locale
      locale = CocoaMessage(0, 0, "NSLocale currentLocale", 0)
      result.s = PeekS(CocoaMessage(0, CocoaMessage(0, locale, "decimalSeparator", 0), "UTF8String"), -1, #PB_UTF8)
      
    CompilerCase #PB_OS_Linux
      Protected *info.lconv, *locale
      *locale = setlocale(#LC_ALL, 0)
      setlocale(#LC_ALL, #Empty$)
      *info = localeconv()
      setlocale(#LC_ALL, *locale)
      result = PeekS(*info\decimal_point , 1, #PB_Ascii)
      
  CompilerEndSelect
  
  ProcedureReturn result
  
EndProcedure

; *****************************************************************************

Procedure.s GetLocaleThousend()
  Protected result.s
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      Protected r1.s{2}
      If GetLocaleInfo_(#LOCALE_USER_DEFAULT, #LOCALE_STHOUSAND, @r1, 2)
        result = r1
      Else
        result = ","
      EndIf
      
    CompilerCase #PB_OS_MacOS
      Protected locale
      locale = CocoaMessage(0, 0, "NSLocale currentLocale", 0)
      result.s = PeekS(CocoaMessage(0, CocoaMessage(0, locale, "groupingSeparator", 0), "UTF8String"), -1, #PB_UTF8)
      
    CompilerCase #PB_OS_Linux
      Protected *info.lconv, *locale
      *locale = setlocale(#LC_ALL, 0)
      setlocale(#LC_ALL, #Empty$)
      *info = localeconv()
      setlocale(#LC_ALL, *locale)
      result = PeekS(*info\thousands_sep , 1, #PB_Ascii)
      
  CompilerEndSelect
  
  ProcedureReturn result
  
EndProcedure

; *****************************************************************************

Global LocalDecimal.s = GetLocaleDecimal()
Global LocalThousend.s = GetLocaleThousend()

Procedure.s LocaleStrF(fltVal.f, decimals=-1)
  If decimals >= 0
    ProcedureReturn ReplaceString(StrF(fltVal, decimals), ".", LocalDecimal)
  Else
    ProcedureReturn ReplaceString(StrF(fltVal), ".", LocalDecimal)
  EndIf  
EndProcedure

; -----------------------------------------------------------------------------

Procedure.s LocaleStrD(dblVal.d, decimals=-1)
  If decimals >= 0
    ProcedureReturn ReplaceString(StrD(dblVal, decimals), ".", LocalDecimal)
  Else
    ProcedureReturn ReplaceString(StrD(dblVal), ".", LocalDecimal)
  EndIf  
EndProcedure

; -----------------------------------------------------------------------------

; *****************************************************************************

;- Test

CompilerIf #PB_Compiler_IsMainFile
  
  Define fVal.f, dVal.d
  
  Debug "Locale format of Float"
  fVal = 1.2345
  Debug LocaleStrF(fVal, 4)
  PokeL(@fVal, $FFFFFFFF)
  Debug LocaleStrF(fVal)
  
  Debug "Locale format of Double"
  dVal = 123456.123456
  Debug LocaleStrD(dVal)
  PokeQ(@dVal, $FFFFFFFFFFFFFFFF)
  Debug LocaleStrD(dVal)
  
  dVal = 1200300.5678
  Debug "Locale Format Number"
  Debug FormatNumber(dVal, 4, LocalDecimal, LocalThousend)
    
CompilerEndIf
Alles ist möglich, fragt sich nur wie...
Projekte ThreadToGUI / EventDesigner V3 / OOP-BaseClass-Modul
Downloads auf MyWebspace / OneDrive
Benutzeravatar
Sicro
Beiträge: 955
Registriert: 11.08.2005 19:08
Kontaktdaten:

Re: GetLocaleDecimal (All OS)

Beitrag von Sicro »

Nützlicher Code :)

Wenn du deinem Code noch eine Lizenz hinzufügst, kann ich ihn zum CodeArchiv hinzufügen.
MIT-Lizenztext-Vorlagen findest du bei den Codes im CodeArchiv.

Noch ein Tipp:
Anstatt deinen Code doppelt zu pflegen (deutscher und englischer Forum-Beitrag), könntest du
den Code hier entfernen und einfach ein Link zum englischem Forum-Beitrag einfügen.
Bild
Warum OpenSource eine Lizenz haben sollte :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (Syntax-Farbschema) :: RegEx-Engine (kompiliert RegExes zu NFA/DFA)
Manjaro Xfce x64 (Hauptsystem) :: Windows 10 Home (VirtualBox) :: Neueste PureBasic-Version
Antworten