Jump to content

[SOLVED] numbers to text


xoligy

Recommended Posts

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);
}
?>

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;   
}

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.