截断Erlang中的浮点数 [英] Truncate a float in Erlang
本文介绍了截断Erlang中的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个函数从一个浮点数创建一个列表。
float_to_list(0.02)。
它返回:
2.00000000000000000000e-002
我需要它给我一个确切的数字如:
0.20
如果我喂它5.23
5.23
如果我喂它5.5
5.50
所以基本上舍入到小数点后两位的数字。
可能是一个简单的修复。
谢谢
编辑:
我想使用它可能会工作的io格式,
但它不在本例中:
wxTextCtrl:setValue(TcGrossProfit,io:format(〜p,[NUMBER])),
似乎textctrl想要一个字符串,我不想打印到屏幕。
解决方案
你正在寻找这样的东西:
6> F = 5/2。
2.50000
7> io_lib:格式( 〜.1F,[F])。
[2.5]
8> io_lib:格式( 〜.2f,[F])。
[2.50]
9> io_lib:格式( 〜.3f,[F])。
[2.500]
如果是,请查看模块io_lib。 / p>
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.
解决方案
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 module io_lib.
这篇关于截断Erlang中的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文