New

Monday, August 10, 2015

how to convert numeric values to 1k 2k php

PHP number abbreviator

I have a database with a column containing a variety of numbers written in "shorthand", for example: 5k for 5,000 86.6k for 86,600 4.1m for 4,100,000 1.2b for 1,200,000,000 I'd like to do some calculations with these numbers for a PHP frontend, but I need to convert them into valid integers in order to do so. How could I do this with PHP?

  • Working example 1

<?php
$tab1 = "460000006";
$tab2 = "100";
$tab3 = "1000";
$tab4 = "10000";
$tab5 = "100000";
function con ($tab) {
if ($tab < 1000) {
$tab = $tab;
echo $tab;
}
else {
if ($tab >= 1000 && $tab < 1000000) {
$tab = $tab / 1000;
$tab = round($tab, 2) . "K";
echo $tab;
}
if ($tab > 1000000) {
$tab = $tab / 1000000;
$tab = round($tab, 2) . "M";
echo $tab;
}
 }
}
//call function anywhere you want
echo con($tab1);
?>


No comments:

Post a Comment