Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

list - Truncate a float in Erlang

I am using a function to create a list from a float.

 float_to_list(0.02).

It returns:

"2.00000000000000000000e-002"

I need it to give me a number exactly like:

"0.20"

If I fed it 5.23

"5.23"

If I fed it 5.5

"5.50"

So basically the number rounded to two decimal places. Probably an easy fix.

Thanks

EDIT:

I would like to use the io format it looks like it might work,

but it dosen't in this example:

wxTextCtrl:setValue( TcGrossProfit, io:format("~p", [NUMBER]), ),

seems textctrl wants a string, I don't want to print it to the screen.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Are you looking for something like this:

6> F = 5/2.
2.50000
7> io_lib:format("~.1f",[F]).
["2.5"]
8> io_lib:format("~.2f",[F]).
["2.50"]
9> io_lib:format("~.3f",[F]).
["2.500"]

If yes, have a look at the io_lib module.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...