Skip to content

Instantly share code, notes, and snippets.

@rbngzlv
Forked from mlersch/currency.erl
Created February 5, 2018 18:19
Show Gist options
  • Save rbngzlv/6549e95312cb10fad50638a1ac6d77cd to your computer and use it in GitHub Desktop.
Save rbngzlv/6549e95312cb10fad50638a1ac6d77cd to your computer and use it in GitHub Desktop.
A Erlang snippet to convert a currency strings or binary to a integer
-author("mlersch").
-module(currency).
-export([test/0, convert/1]).
convert(V) when is_binary(V) -> convert(binary_to_list(V));
convert(V) when is_float(V) -> round(V*100);
convert(V) when is_integer(V) -> V*100;
convert(V) when is_list(V) ->
V1 = re:replace(re:replace(V, "[^0-9 ^, ^.]", "", [{return,list},unicode,global]), "\\h", "", [{return,list},global]),
convert_strip_a(
list_to_integer(re:replace(V1, "[\\. ,]", "", [{return,list},global])),
string:rchr(V1, $,),
string:rchr(V1, $.),
length(string:substr(V1, string:rchr(V1, $,)+1)),
length(string:substr(V1, string:rchr(V1, $.)+1))
).
%% when no "," or "."
convert_strip_a(V, KomPos, DotPos, _KomRest, _DotRest)
when KomPos == 0
andalso DotPos == 0
-> V * 100;
%% last is "," and no "."
convert_strip_a(V, KomPos, DotPos, KomRest, _DotRest)
when KomPos > DotPos
-> convert_strip_b(V, KomRest);
%% last is "." and no ","
convert_strip_a(V, KomPos, DotPos, _KomRest, DotRest)
when KomPos < DotPos
-> convert_strip_b(V, DotRest).
%% 100, or 100,000
convert_strip_b(V, Rest) when Rest == 0 orelse Rest == 3 -> V * 100;
%% 100,0
convert_strip_b(V, Rest) when Rest == 1 -> V * 10;
%% 100,00
convert_strip_b(V, Rest) when Rest == 2 -> V.
test()->
%% Testing floats and integers
10000 = convert(100),
10000 = convert(100.0),
10000 = convert(100.00),
%% Testing Regular strings
10000 = convert("100"),
10000 = convert("100,0"),
10000 = convert("100,00"),
10000 = convert("100.0"),
10000 = convert("100.00"),
100000 = convert("1000,00"),
100000 = convert("1000.00"),
100000 = convert("1.000,00"),
100000 = convert("1,000.00"),
100000 = convert("1,000"),
100000 = convert("1.000"),
100000000 = convert("1000000,00"),
100000000 = convert("1000000.00"),
100000000 = convert("1.000.000,00"),
100000000 = convert("1,000,000.00"),
100000000 = convert("1,000,000"),
100000000 = convert("1.000.000"),
%% Testing unicode chars
10000 = convert("100 €"),
10000 = convert("100,0 €"),
10000 = convert("100,00 €"),
10000 = convert("100.0 €"),
10000 = convert("100.00 €"),
100000 = convert("1000,00 €"),
100000 = convert("1000.00 €"),
100000 = convert("1.000,00 €"),
100000 = convert("1,000.00 €"),
100000 = convert("1,000 €"),
100000 = convert("1.000 €"),
100000000 = convert("1000000,00 €"),
100000000 = convert("1000000.00 €"),
100000000 = convert("1.000.000,00 €"),
100000000 = convert("1,000,000.00 €"),
100000000 = convert("1,000,000 €"),
100000000 = convert("1.000.000 €"),
%% Testing more text and prefixes
10000 = convert("$100more text like $ۤ?#"),
10000 = convert("$100,0more text like $ۤ?#"),
10000 = convert("$100,00more text like $ۤ?#"),
10000 = convert("$100.0more text like $ۤ?#"),
10000 = convert("$100.00more text like $ۤ?#"),
100000 = convert("$1000,00more text like $ۤ?#"),
100000 = convert("$1000.00more text like $ۤ?#"),
100000 = convert("$1.000,00more text like $ۤ?#"),
100000 = convert("$1,000.00more text like $ۤ?#"),
100000 = convert("$1,000more text like $ۤ?#"),
100000 = convert("$1.000more text like $ۤ?#"),
100000000 = convert("$1000000,00more text like $ۤ?#"),
100000000 = convert("$1000000.00more text like $ۤ?#"),
100000000 = convert("$1.000.000,00more text like $ۤ?#"),
100000000 = convert("$1,000,000.00more text like $ۤ?#"),
100000000 = convert("$1,000,000more text like $ۤ?#"),
100000000 = convert("$1.000.000more text like $ۤ?#"),
%% Testing irritating Chars and wrong separators
100000 = convert("1,000,00"),
100000 = convert("1.000.00"),
100000 = convert("1,000,-"),
100000 = convert("1.000,-"),
100000 = convert("1,000,0-"),
100000 = convert("1.000.0-"),
100000 = convert("1,000,0-"),
%% Test Ignore wrong thousand separators
10000 = convert("1.00,00"),
10000 = convert("1,00,00"),
10000 = convert("1,00.00"),
10000 = convert("1.00.00"),
1000000 = convert("1.00.00,00"),
1000000 = convert("1,00,00,00"),
1000000 = convert("1,00,00.00"),
1000000 = convert("1.00.00.00"),
%% Test Ignore wrong thousand separators with 1 digit cent
10000 = convert("1.00,0"),
10000 = convert("1,00,0"),
10000 = convert("1,00.0"),
10000 = convert("1.00.0"),
1000000 = convert("1.00.00,0"),
1000000 = convert("1,00,00,0"),
1000000 = convert("1,00,00.0"),
1000000 = convert("1.00.00.0"),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment