Jump to content

number format


Recommended Posts

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

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

Link to comment
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]
Link to comment
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
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?
Link to comment
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.
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.

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.