Align decimal places

Contents

Output

2.12

Code examples


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;