xoligy Posted September 24, 2008 Share Posted September 24, 2008 ok ive looked at a few online games and noticed that when a number gets high the end will be made into bil so if someone has 19.450.000.000 it show as 19.45bil or 195.45bil now this is something i would like to implement on a game too. Now i managed to find a number to text converter but it converts everything! How would i go about making it work how i want? Here is the code i found anyhow: <?php // Hugh Bothwell hugh_bothwell@hotmail.com // August 31 2001 // Number-to-word converter $ones = array( "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" ); $tens = array( "", "", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" ); $triplets = array( "", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion", " sextillion", " septillion", " octillion", " nonillion" ); // recursive fn, converts three digits per pass function convertTri($num, $tri) { global $ones, $tens, $triplets; // chunk the number, ...rxyy $r = (int) ($num / 1000); $x = ($num / 100) % 10; $y = $num % 100; // init the output string $str = ""; // do hundreds if ($x > 0) $str = $ones[$x] . " hundred"; // do ones and tens if ($y < 20) $str .= $ones[$y]; else $str .= $tens[(int) ($y / 10)] . $ones[$y % 10]; // add triplet modifier only if there // is some output to be modified... if ($str != "") $str .= $triplets[$tri]; // continue recursing? if ($r > 0) return convertTri($r, $tri+1).$str; else return $str; } // returns the number as an anglicized string function convertNum($num) { $num = (int) $num; // make sure it's an integer if ($num < 0) return "negative".convertTri(-$num, 0); if ($num == 0) return "zero"; return convertTri($num, 0); } ?> and a test fn I wrote, <?php function randThousand() { return mt_rand(0,999); } // Returns an integer in -10^9 .. 10^9 // with log distribution function makeLogRand() { $sign = mt_rand(0,1)*2 - 1; $val = randThousand() * 1000000 + randThousand() * 1000 + randThousand(); $scale = mt_rand(-9,0); return $sign * (int) ($val * pow(10.0, $scale)); } // example of usage for ($i = 0; $i < 20; $i++) { $num = makeLogRand(); echo "<br>$num: ".convertNum($num); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/ Share on other sites More sharing options...
jonsjava Posted September 24, 2008 Share Posted September 24, 2008 I'll let someone clean this up, but here's a way: <?php $score = "90000000000000"; $num_array[9] = "billion"; $num_array[13] = "trillion"; $num_array[16] = "quadrillion"; $score_nums = strlen($score); if ($score_nums >= 9 && $score_nums < 13){ $score = $score / 1000000000; $score = $score." ".$num_array[9]; } elseif ($score_nums >= 13 && $score_nums < 16){ $score = $score / 1000000000000; $score = $score." ".$num_array[13]; } elseif ($score_nums >= 16 && $score_nums < 19){ $score = $score / 1000000000000000; $score = $score." ".$num_array[16]; } print $score; ?> Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-649392 Share on other sites More sharing options...
xoligy Posted September 24, 2008 Author Share Posted September 24, 2008 Thanks jonsjava, i'll see if i can get it to work now and report back. I maybe some time lol Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-649457 Share on other sites More sharing options...
xoligy Posted September 24, 2008 Author Share Posted September 24, 2008 Your way seems to work, in a way and not in another lol what ive done is where you had $score = 9000000000000 ive added $data[4] and this is the result: $155.181680404 trillion $90.25031693 billion and this is 5,000 $5000 how would i make it so that its just 155.18tril, 90.25bil and 5,000 or 5.000? Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-649499 Share on other sites More sharing options...
jonsjava Posted September 24, 2008 Share Posted September 24, 2008 you mean something like this: <?php $score = "1551816801.404"; $score = round($score); $num_array[10] = "billion"; $num_array[13] = "trillion"; $num_array[16] = "quadrillion"; $score_nums = strlen($score); if ($score_nums >= 10 && $score_nums < 13){ $score = $score / 1000000000; $score = round($score, 2); $score = $score." ".$num_array[10]; } elseif ($score_nums >= 13 && $score_nums < 16){ $score = $score / 1000000000000; $score = round($score, 2); $score = $score." ".$num_array[13]; } elseif ($score_nums >= 16 && $score_nums < 19){ $score = $score / 1000000000000000; $score = round($score, 2); $score = $score." ".$num_array[16]; } print $score; ?> If this isn't what you wanted, let me know. Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-649749 Share on other sites More sharing options...
xoligy Posted September 24, 2008 Author Share Posted September 24, 2008 Will let you know tomorrow jonsjava laptop is out of commission at the moment Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-649805 Share on other sites More sharing options...
xoligy Posted September 25, 2008 Author Share Posted September 25, 2008 Seems to of done the trick jonsjava thanks very much! Solved! Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-650336 Share on other sites More sharing options...
xoligy Posted September 25, 2008 Author Share Posted September 25, 2008 Ok this is solved, but i have a new error i put the above code into a function as its going to get used abit i then went to my ranks page and called the function and all was great! But for some reason its not changing one of the fields its ment to anyone figure out why? <?=int2txt($t10[2])?> <?=int2txt($t10[3])?> <---- this value is coming out as numbers if i replace int2txt with commas which places a , ever 3rd nuber that works strange <?=int2txt($t10[4])?> ________________________ function int2txt($data){ $score = $data; $score = round($score); $num_array[6] = "Million"; $num_array[10] = "Billion"; $num_array[13] = "Trillion"; $num_array[16] = "Quadrillion"; $num_array[20] = "Quintillion"; $score_nums = strlen($score); if ($score_nums >= 6 && $score_nums <9){ $score = $score / 100000; $score = round($score, 2); $score = $score." ". $num_array[6]; } elseif ($score_nums >= 10 && $score_nums < 13){ $score = $score / 1000000000; $score = round($score, 2); $score = $score." ".$num_array[10]; } elseif ($score_nums >= 13 && $score_nums < 16){ $score = $score / 1000000000000; $score = round($score, 2); $score = $score." ".$num_array[13]; } elseif ($score_nums >= 16 && $score_nums < 19){ $score = $score / 1000000000000000; $score = round($score, 2); $score = $score." ".$num_array[16]; } elseif ($score_nums >= 20 && $score_nums < 23){ $score = $score / 10000000000000000000; $score = round($score, 2); $score = $score." ".$num_array[20]; } print $score; } Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-650349 Share on other sites More sharing options...
jonsjava Posted September 25, 2008 Share Posted September 25, 2008 didn't calculate the zeroes in the numbers properly. <?=int2txt($t10[2])?> <?=int2txt($t10[3])?> <---- this value is coming out as numbers if i replace int2txt with commas which places a , ever 3rd nuber that works strange <?=int2txt($t10[4])?> <?php function int2txt($data){ $score = $data; $score = round($score); $num_array[7] = "Million"; $num_array[10] = "Billion"; $num_array[13] = "Trillion"; $num_array[16] = "Quadrillion"; $num_array[19] = "Quintillion"; $score_nums = strlen($score); if ($score_nums >= 7 && $score_nums <10){ $score = $score / 100000; $score = round($score, 2); $score = $score." ". $num_array[7]; } elseif ($score_nums >= 10 && $score_nums < 13){ $score = $score / 1000000000; $score = round($score, 2); $score = $score." ".$num_array[10]; } elseif ($score_nums >= 13 && $score_nums < 16){ $score = $score / 1000000000000; $score = round($score, 2); $score = $score." ".$num_array[13]; } elseif ($score_nums >= 16 && $score_nums < 19){ $score = $score / 1000000000000000; $score = round($score, 2); $score = $score." ".$num_array[16]; } elseif ($score_nums >= 19 && $score_nums < 22){ $score = $score / 1000000000000000000; $score = round($score, 2); $score = $score." ".$num_array[19]; } print $score; } Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-650444 Share on other sites More sharing options...
xoligy Posted September 25, 2008 Author Share Posted September 25, 2008 Yea i managed to fix that error, now working on why im getting 0.5million and 0.53billion :-\ Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-650468 Share on other sites More sharing options...
xoligy Posted September 25, 2008 Author Share Posted September 25, 2008 Sorted all my issues thanks again jonsjava Just had to change >= to > and < to <= Quote Link to comment https://forums.phpfreaks.com/topic/125593-solved-numbers-to-text/#findComment-650473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.