1

I have strings of numbers like this:

- 00986756849

- 007478599700

- 004583930237345

I need to convert these strings of numbers into new numbers with the last two decimal values in PHP, ie:

- 009867568.49

- 0074785997.00

- 0045839302373.45

How to?

Muhammad Bilal
  • 2,106
  • 1
  • 15
  • 24
  • 2
    possible duplicate of [Formatting a number with leading zeros in PHP](http://stackoverflow.com/questions/1699958/formatting-a-number-with-leading-zeros-in-php) – Ruslan Ostafiichuk Jan 02 '15 at 10:11
  • 1
    @RuslanOstafiychuk His question isn't about leading zeroes, it's about 2 decimal digits. – Barmar Jan 02 '15 at 10:14
  • you should explode the last two digits and then concate "." value – Arun Jan 02 '15 at 10:15

3 Answers3

1

Get the string value . use substr_replace to replace a sub string or insert a substring to a specific position in the string :)

$str = "00986756849";

substr_replace($str,".",strlen($str)-2,0);
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
0
$intNumber = (int) $stringNumber;
$decimalNumber = sprintf('%02d', $intNumber);
Kiren S
  • 3,037
  • 7
  • 41
  • 69
0

You can try this

//$str = "10";
$str = "00986756849";
$str_length = strlen($str);
if($str_length>2)
{
    $output = substr_replace($str,".",$str_length-2,0);
}
else
{
    $output = substr_replace($str,".00",$str_length,0);
}
echo $output;
GIPSSTAR
  • 2,050
  • 1
  • 25
  • 20