|
Re: string conversion, format functions: msg#00337lang.erlang.general
On Wed, 23 Apr 2003 16:52:45 -0400 (EDT) HP Wei <hp@xxxxxxxxxx> wrote: > I have a string "123" and want to convert it to an integer > or float, and vice versa. > > In python, I go to the string module and > there are string.atoi(), string.atof(), and str(number). > So, by habit, I went to string module doc in Erlang > I could not find the counterparts of these functions. Where are they ? They're BIFs (Built In Functions); they can also be found in the module 'erlang': list_to_integer(String) list_to_float(String) integer_to_list(Integer) etc. btw, there is no integer_to_float; use coercion (Integer + 0.0 works) Also, converting the string "123" into a float will fail. > ------------------------- > B = 3. > > I want to form a string "0003". > ** Can io:format generate such a printout ? Hmmm... good question! If it can, it's not documented. (Why is there no formatting code for integers in io:format?) I tried 17> io:fwrite("~5.1.0f~n", [3 + 0.0]). 003.0 ok 18> io:fwrite("~5.0.0f", [3 + 0.0]). ** exited: {format,[{io,format,[<0.21.0>,"~5.0.0f",[3.00000]]}, {erl_eval,expr,3}, {erl_eval,exprs,4}, {shell,eval_loop,2}]} ** I also tried 20> io:fwrite("~5.0.0p~n", [3]). 3 ok No luck... > ** Can it be redirected to a string ? Yes, see io_lib:format/2. > ** If not, how do folks in Erlang world do this padding? I guess the best way right now is: 23> string:right(integer_to_list(3), 4, $0). "0003" > thanks, > hp Hope that helps, -Chris |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | string conversion, format functions: 00337, HP Wei |
|---|---|
| Next by Date: | Re: string conversion, format functions: 00337, Matthias Lang |
| Previous by Thread: | string conversion, format functionsi: 00337, HP Wei |
| Next by Thread: | Re: string conversion, format functions: 00337, Matthias Lang |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |