[erlang-questions] simple question: how to convert integers to string with fixed digits

Håkan Stenholm <>
Sat Dec 1 13:03:28 CET 2007


Matej Kosik wrote:

You can experiment with the io:format(...) (or io_lib:format(...)) 
string, this works in you case:

6> io:format("~2..0w", [2]).
02

10> io:format("~2.16.0B", [255]).
FF

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello,
>
> In Squeak there is a method
>
> 	Integer > printing-numerative > printStringLength:padded:
>
> It can be used as follows. Evaluation of
>
> 	2 printStringLength: 2 padded: true
>
> yields
>
> 	'02'
>
> Evaluation of
>
> 	12 printStringLength: 2 padded: true
>
> yields
>
> 	 '12'
>
> Is there an analogous function in Erlang? Note that I am not looking for
>
> 	integer_to_list/1
>
> because I need to get strings of "fixed length" padded with zeros on the left. Is there directly
> something similar or should I write my own such function?
>
> NOTE: I would like to print dates and times into my logs.
> - -----------------------------------------------------------------------------------------------
> Similar question.
>
> In Squeak there is a method
>
> 	Integer > printing-numerative > printStringBase:length:padded:
>
> Evaluation of
>
> 	11 printStringBase: 16 length: 2 padded: true
>
> yields
>
> 	'0B'
>
> Evaluation of
>
> 	255 printStringBase: 16 length: 2 padded: true
>
> yields
>
> 	'FF'
>
> Is there analogous function in Erlang? The `format' string already has support for printing
> hexadecimals but not of "fixed length". Am I right?
>
> NOTE: I would like to print MAC addresses in a standard way.
>
> Thanks in advance.
> - --
> Matej Kosik
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFHUUVZL+CaXfJI/hgRAtvPAJ4y0vDG0o4VhDpyU8+ev4+ZVeKtHQCfbmi1
> MEuA/gT9N4zljPALq0dsURA=
> =q+a1
> -----END PGP SIGNATURE-----
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>   



More information about the erlang-questions mailing list