An Erlang function to truncate a floating point number to N decimal places ignoring trailing zeros.

-module(ql).
-export([trunc/2]).
 
%% Truncates a floating point number F to N decimal places ignoring trailing zeros.
-spec trunc(float()|integer(), integer()) -> float().
trunc(F, N) ->
    Prec = math:pow(10, N),
    trunc(F*Prec)/Prec
1> c(ql).
{ok,ql}
 
2> ql:trunc(3.1415926525, 4).
3.1415
 
3> ql:trunc(3.1400, 4).      
3.14
 
4> ql:trunc(3.0, 4).   
3.0
 
5> ql:trunc(3, 4).  
3.0