- 论坛徽章:
- 27
|
本帖最后由 shijiang1130 于 2014-09-14 23:01 编辑
- -module(separator).
- -compile(export_all).
- partition_number(Number, Separator) ->
- SepChar = hd(Separator),
- NumStr = if
- is_integer(Number) -> hd(io_lib:format("~B", [Number]));
- is_float(Number) -> hd(io_lib:format("~f", [Number]));
- true -> Number
- end,
- % If there is a decimal point, use that as the starting point
- {ChgStr, Remain} = case re:run(NumStr, "\\.") of
- {match,Start,_} ->
- {string:substr(NumStr, 1, Start - 1),
- string:substr(NumStr,Start,string:len(NumStr))};
- nomatch -> {NumStr, ""}
- end,
- {ok, Subs, _} = regexp:sub(lists:reverse(ChgStr),
- "([0-9][0-9][0-9])", "&" ++ Separator),
- Final = case lists:nth(string:len(Subs), Subs) of
- SepChar -> string:substr(Subs, 1, string:len(Subs) - 1);
- _ -> Subs
- end,
- lists:reverse(Final) ++ Remain.
复制代码 |
|