GetLocaleDecimal (All OS)

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

GetLocaleDecimal (All OS)

Post 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
Last edited by mk-soft on Sat Jun 30, 2018 4:01 pm, edited 4 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: GetLocaleDecimals (All OS)

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: GetLocaleDecimals (All OS)

Post by Kwai chang caine »

Thanks MKSOFT for sharing 8)
Like usually.. i have not understand the usefulness :oops:
ImageThe happiness is a road...
Not a destination
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GetLocaleDecimals (All OS)

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: GetLocaleDecimals (All OS)

Post by Kwai chang caine »

Aaaah ok!!! thanks MkSoft for your explanation 8)
ImageThe happiness is a road...
Not a destination
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GetLocaleDecimals (All OS)

Post by mk-soft »

Update v1.1
- Added GetLocaleThousend

and example with FormatNumber(...)

:wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply