How to convert float to string with 2 decimal places in Ruby

2 Answers

0 votes
f = 787324.9761;

s = "%.2f" % f;

print s;



# run:
#
# 787324.98
#

 



answered Feb 13, 2023 by avibootz
0 votes
f = 787324.9761;

s = f.round(2);

print s;



# run:
#
# 787324.98
#

 



answered Feb 13, 2023 by avibootz
...