Jump to content

Archived

This topic is now archived and is closed to further replies.

blacknight

number format

Recommended Posts

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?

Share this post


Link to post
Share on other sites
...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?

Share this post


Link to post
Share on other sites
As long as 25 is in range of 00-99 then this should work:
[code=php:0]<?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;

?>[/code]

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
Yes use list and explode like so:
[code=php:0]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";[/code]


That replaces this:
[code=php:0]// disply the number:
echo 'Old: ' . $num . "<br />\n";
echo 'New: ' . $num_formated;[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.