Contents [hide]
Output
2.12
Code examples
JavaScript
1 2 | const val = 2.123456789 console.log(val.toFixed(2)) |
PHP
1 2 3 | <?php $val = 2.123456789; print sprintf( "%.2f" , $val ). "\n" ; |
Perl
1 2 | my $val = 2.123456789; print sprintf ( "%.2f" , $val ). "\n" ; |
Python
1 2 | val = 2.123456789 print ( '{:.2f}' . format (val)) |
Ruby
1 2 | val = 2 . 123456789 puts sprintf( "%.2f" , val) |
Shell
1 2 3 | VAL=2.123456789; RESULT=` printf "%.2f" $VAL`; echo $RESULT; |
Author: Hajime Kurita, CEO/CTO of 1st Class, inc. Search Engineer & Web master
Certificate: AWS SOL Architect, Professional Scrum Master I, Class I IT Engineer(JP national certificate), TOEIC900+, Graduate of Tokyo university
Please follow My English twitter account for updates
IT Relatd service: VPS Ranking