use strict; use warnings; sub fmthd { # number, decimal separator, thousands separator my ( $dec, $thd ) = ( $_[1] // ".", $_[2] // "," ); my ( $n, $d ) = split /\Q$dec\E/, shift; my $s = $n<0?"-":""; $n = reverse $n; return $s.reverse( $n = join $thd, $n =~ /(\d{1,3})/g ).($d?"$dec$d":"${dec}00"); } print fmthd( 1234567.14 ), "\n"; print fmthd( 1234567 ), "\n"; print fmthd( 12.14 ), "\n"; print fmthd( 123456.14343 ), "\n"; print fmthd( 1234567.14 ), "\n"; print fmthd( 8881234567.14 ), "\n"; print fmthd( -8881234567.14 ), "\n"; print fmthd( "-8881234567,14", ",", "." ), "\n";