Free coupon for initial registration

Align decimal places

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;
Free coupon for initial registration