Contents
输出结果
2.12
程式码
JavaScript
const val = 2.123456789 console.log(val.toFixed(2))
PHP
<?php $val = 2.123456789; print sprintf("%.2f", $val)."\n";
Perl
my $val = 2.123456789; print sprintf("%.2f", $val)."\n";
Python
val = 2.123456789 print('{:.2f}'.format(val))
Ruby
val = 2.123456789 puts sprintf("%.2f", val)
Shell
VAL=2.123456789; RESULT=`printf "%.2f" $VAL`; echo $RESULT;