Jump to content

number format


blacknight

Recommended Posts

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]
Link to comment
https://forums.phpfreaks.com/topic/18697-number-format/#findComment-80748
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
https://forums.phpfreaks.com/topic/18697-number-format/#findComment-80766
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?
Link to comment
https://forums.phpfreaks.com/topic/18697-number-format/#findComment-81020
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
https://forums.phpfreaks.com/topic/18697-number-format/#findComment-81022
Share on other sites

Archived

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

×
×
  • 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.