Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 374
BAR Avatar asked Oct 23 '10 21:10

BAR


1 Answers

Alternatively you could use the function you were already using.

float_to_list(0.02,[{decimals, 2}]) outputs '0.02'

Or for Elixir users ;)

:erlang.float_to_list(5.231,[{:decimals, 2}]) outputs '5.2'

like image 193
Pure Function Avatar answered Sep 18 '22 08:09

Pure Function