Jump to content


Photo

number format


  • Please log in to reply
9 replies to this topic

#1 blacknight

blacknight
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • Locationsaint john

Posted 26 August 2006 - 07:15 AM

ok i need to format a number string foing from the right say example
62500 need to be formated to 6 25 00 reasion for being this is for a game site where the 00 is lesser then the 25 and the 6 can go as high as 9999 any ideas?
if at first your fail google will give you a pass

#2 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 26 August 2006 - 07:45 AM

...what...?

so according to you, you want 99992524 to be formatted to 9999 25 24?
and use the same code to change
62500 to 6 25 00

am I correct?

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 August 2006 - 02:43 PM

As long as 25 is in range of 00-99 then this should work:
<?php

$num = '62500';

// reverse the number so we can work right to left
$num_rev = strrev($num);

// format the number
// number fomrat in reverse
// xx yy zzzz
$num_format_rev = preg_replace("/([0-9]{2})([0-9]{2})([0-9]{1,4})/", "$1 $2 $3", $num_rev);

// now reverse the formated number
$num_formated = strrev($num_format_rev);

// disply the number:
echo 'Old: ' . $num . "<br />\n";
echo 'New: ' . $num_formated;

?>


#4 blacknight

blacknight
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • Locationsaint john

Posted 26 August 2006 - 03:44 PM

ok that achived the desired affect now to go farther is there a way to seperate the numbers in to there own veriables like $var1 $var2 $var3 ?
if at first your fail google will give you a pass

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 August 2006 - 03:48 PM

Yes use list and explode like so:
list($num1, $num2, $num3) = explode(" ", $num_formated);

echo 'Number 1: ' . $num1 . "<br />\n";
echo 'Number 2: ' . $num2 . "<br />\n";
echo 'Number 3: ' . $num3 . "<br />\n";


That replaces this:
// disply the number:
echo 'Old: ' . $num . "<br />\n";
echo 'New: ' . $num_formated;


#6 blacknight

blacknight
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • Locationsaint john

Posted 26 August 2006 - 03:54 PM

ok we got er thanks guys we needed this for a world of warcraft guild page for reward money for quests :) the fancarian solution thanks you
if at first your fail google will give you a pass

#7 blacknight

blacknight
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • Locationsaint john

Posted 27 August 2006 - 05:45 AM

ook we finaly hit a snage when a veriiable id 4 characters or less it automaticalyt asumes its all one number is there a fix for teis
so
5525 reads as 55 25 not 5525 as the first number where 55 should be the second and 25 the 3rd and the 1st being empty?
if at first your fail google will give you a pass

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 27 August 2006 - 05:54 AM

You need to explain just what rules you want to be followed in all circumstances.  Reading your first post - '6 can go as high as 9999' - but now you can have a 4-digit number that ought to be split into two numbers, not three.  You might want to reconsider how the numbers are formatted and de-limit them in the first place as the 'rules' for how to split them sound arbitrary.
Legend has it that reading the manual never killed anyone.
My site

#9 blacknight

blacknight
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • Locationsaint john

Posted 27 August 2006 - 07:11 AM

yes i understand the numbers axualt represent money being 9999 gold peaces 99 sulver and 99 bronze being the most someone can get and 1 bronze being the least i will see what i can figure out from the code given to me...
if at first your fail google will give you a pass

#10 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 27 August 2006 - 08:21 AM

you should just be able to have a defaulted number pattern
like 00000000

and then just extract it like you are so you'll never have a 4 character line


btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users