Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Currency and decimal convertor
#1
Is there any way to convert currency and decimals values from any language to English(us) quickly? without having them change the regional and language settings in windows?
#2
Like "12,34" string to 12.34 number?
#3
yes and currency as well but the exact regional and language setting may be unknown so i was looking for a way to convert everything into English(us ) format
#4
let's assume that you have Lithuanian format in control panel -> regional
Macro
Code:
Copy      Help
str s="12,34 Lt" ;;Lithuanian currency format. Also can be without Lt, start with Lt, etc, but fails if it is some other currency

CURRENCY cy
int hr=VarCyFromStr(@s LOCALE_USER_DEFAULT 0 &cy)
if(hr) end _s.dllerror("" "" hr)
out cy

;QM also uses VarCyFromStr, but this and other QM type conversion functions
;always use fixed locale 0x409 (English US), not LOCALE_USER_DEFAULT.

;Google for:
;site:microsoft.com Locale Constants
#5
ok maybe i'm missing something but that example makes it 1234 not 12.34 what i was looking for was a way to convert a string from any language to english(us) format currency or decimal.Convert it to that and then how convert it back to the format it was if needed. examples in german 500.000= 500,000 in us
#6
My example converts from user default format (which is set in control panel) to US format.

You cannot convert from any format if you don't know it and don't use user default format.

For example, in "500,000", how you can know is the , decimal or thousands separator?
#7
Macro
Code:
Copy      Help
str s
int hr

;--- currency ---

inp- s "a number in format that is set on your computer in control panel -> regional -> ... -> currency"

CURRENCY cy
hr=VarCyFromStr(@s LOCALE_USER_DEFAULT 0 &cy)
if(hr) end _s.dllerror("" "" hr)
out cy

;--- numbers ---

inp- s "a number in format that is set on your computer in control panel -> regional -> ... -> numbers"

double d
hr=VarR8FromStr(@s LOCALE_USER_DEFAULT 0 &d)
if(hr) end _s.dllerror("" "" hr)
out d

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

;QM also uses these functions but it uses locale 0x409 (English US), not LOCALE_USER_DEFAULT.

;Google for:
;site:microsoft.com Locale Constants
#8
ok i understand now tyvm
#9
Shows how to convert numbers, currency, time and date format from your locale to US locale and back.
Also can be useful as format converter.
Function dlg_locale
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

str controls = "4 6 7"
str e4 e6 cb7
cb7="&Number[]Currency[]Time[]Date"
if(!ShowDialog("dlg_locale" &dlg_locale &controls)) ret

;BEGIN DIALOG
;0 "" 0x90C80A44 0x100 0 0 127 58 "Locale Converter"
;3 Static 0x54000000 0x0 2 22 48 12 "In your locale"
;4 Edit 0x54030080 0x200 56 20 68 14 ""
;5 Static 0x54000000 0x0 2 42 46 12 "In US locale"
;6 Edit 0x54030080 0x200 56 40 68 14 ""
;7 ComboBox 0x54230243 0x0 2 4 54 213 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2030009 "*" "" ""

ret
;messages
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
str s
int hr
BSTR b
double d
CURRENCY c
DATE da
sel wParam
,case CBN_SELENDOK<<16|7
,_s.setwintext(id(4 hDlg)); _s.setwintext(id(6 hDlg))
,
,case [EN_CHANGE<<16|4,EN_CHANGE<<16|6]
,if(GetFocus!lParam) ret
,int tous(wParam&0xffff=4) l1(iif(tous LOCALE_USER_DEFAULT 0x409)) l2(iif(tous 0x409 LOCALE_USER_DEFAULT))
,s.getwintext(lParam)
,if(s.len)
,,b=s
,,sel CB_SelectedItem(id(7 hDlg))
,,,case 0 hr=VarR8FromStr(b l1 0 &d); if(!hr) hr=VarBstrFromR8(d l2 0 &b)
,,,case 1 hr=VarCyFromStr(b l1 0 &c); if(!hr) hr=VarBstrFromCy(c l2 0 &b)
,,,case [2,3] hr=VarDateFromStr(b l1 0 &da); if(!hr) hr=VarBstrFromDate(da l2 0 &b)
,,if(hr) s.dllerror("" "" hr); else s=b
,s.setwintext(id(iif(tous 6 4) hDlg))
,
,case IDOK
,case IDCANCEL
ret 1


Forum Jump:


Users browsing this thread: 1 Guest(s)