این مثال عینا شبیه مثال قبل است با این تفاوت که فرمول محاسبه مبلغ نهایی تفاوت دارد.
A = P (1 + nr )^nt
در این مثال با توجه به اینکه تنها به دو رقم اعشار در جواب نیاز دارم از .2f استفاده کردم که در تابع io:format مشهود است.
-module(ex13). -export([main/0]). -compile([debug_info]). main() -> try run() catch error:_Error -> io:format( "Please Enter only Numeric type~n"); throw:negativeNumber -> io:format( "Please Enter only non-zero positive number~n") end. run() -> {P,R,T,N} = readItems(), A = calc_Investment({P,R,T,N}), io:fwrite( "$~w invested at ~w% for ~w years compounded ~w times per year is $~.2f", [P,R,T,N,A]). calc_Investment({P,R,T,N}) -> P*(math:pow(1+R/(N*100),N*T)). readItems() -> {ok,[P]} = io:fread( "Enter the principal: ","~d"), {R,_} = string:to_float(string:strip( io:get_line("Enter the rate of interest: "), right, $\n)), {ok,[T]} = io:fread( "Enter the number of years: ","~d"), {ok,[N]} = io:fread( "What is the number of times the interest is compounded per year? ","~d"), if P =< 0 orelse R =< 0 orelse T =< 0 orelse N =< 0 -> throw(negativeNumber); true -> ok end, {P,R,T,N}.