phpsplit

Split 4 digit numbers


I want to split a 4 digit number with 4 digit decimal .
Inputs:

Input 1 : 5546.263 
Input 2 : 03739.712  /*(some time may have one zero at first)*/

Result: (array)

Result of input 1 :  0 => 55 , 1 => 46.263
Result of input 2 :  0 => 37 , 1 => 39.712

P.S : Inputs is GPS data and always have 4 digit as number / 3 digit as decimal and some time have zero at first .


Solution

  • You could use the following function:

    function splitNum($num) {
        $num = ltrim($num, '0');
        $part1 = substr($num, 0, 2);
        $part2 = substr($num, 2);
        return array($part1, $part2);
    }
    

    Test case 1:

    print_r( splitNum('5546.263') );
    

    Output:

    Array
    (
        [0] => 55
        [1] => 46.263
    )
    

    Test case 2:

    print_r( splitNum('03739.712') );
    

    Output:

    Array
    (
        [0] => 37
        [1] => 39.712
    )
    

    Demo!