How To Sort Text as a Number in MySQL

select theField from theTable order by theField+0; ~OR~ select theField from theTable order by CAST(theField AS UNSIGNED);
![]() |
select theField from theTable order by theField+0; ~OR~ select theField from theTable order by CAST(theField AS UNSIGNED);
1 2 3 4 5 |
var num = 10; var result = num.toFixed(2); // result will equal 10.00 var num= 170.2305; var result = num.toFixed(3); // result will equal 170.231 |
PHP number_format($number, 2, ‘.’, ‘,’); SMARTY {$number|number_format:2:”.”:”,”}
This solution is hyper-simplistic, and does not deal with decimals or real numbers (those with either a – or a + in front of them)…
1 2 3 4 5 |
if ($value =~ /^\d+$/) { ## $value is an integer } else { ## $value is NOT an integer } |
1 2 |
my $number = 123.45; printf("%.3f", $number); |
Outputs: 123.450
To round up to the nearest 5:
1 2 |
use Math::Round qw(nhimult); $roundedUpNumber = nhimult( 5, $originalNumber ); |
http://search.cpan.org/dist/Math-Round/Round.pm To round up to the nearest 1: Cross-posted from PerlMonks:
1 2 3 4 |
sub roundUp { my $n = shift; return( $n == int($n) ? $n : int($n + 1) ); } |