Page 1 of 1

GetLocaleDecimal (All OS)

Posted: Thu Jun 28, 2018 9:55 am
by mk-soft
With function LocaleStrF(...) and LocaleStrD(...) :wink:

Update v1.1
- Added GetLocaleThousend

Update v1.12
- Bugfix Import C

Code: Select all

;-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

Re: GetLocaleDecimals (All OS)

Posted: Thu Jun 28, 2018 10:07 am
by NicTheQuick
So you are only replacing the point, nothing else? It would be cool to make this thing bigger, more interesting, using all the things which are possible. Like grouping, thousands separator, currency symbol and so on.

Re: GetLocaleDecimals (All OS)

Posted: Thu Jun 28, 2018 2:19 pm
by Kwai chang caine
Thanks MKSOFT for sharing 8)
Like usually.. i have not understand the usefulness :oops:

Re: GetLocaleDecimals (All OS)

Posted: Thu Jun 28, 2018 2:30 pm
by mk-soft
Kwai chang caine wrote:Thanks MKSOFT for sharing 8)
Like usually.. i have not understand the usefulness :oops:
I needed this to copy data (float/double) via the clipboard to Excel.
Excel wants to have the value in German with a comma and not with a dot.

@NicTheQuick
perhaps later

Re: GetLocaleDecimals (All OS)

Posted: Thu Jun 28, 2018 3:40 pm
by Kwai chang caine
Aaaah ok!!! thanks MkSoft for your explanation 8)

Re: GetLocaleDecimals (All OS)

Posted: Sat Jun 30, 2018 1:24 pm
by mk-soft
Update v1.1
- Added GetLocaleThousend

and example with FormatNumber(...)

:wink: