0

I am facing a problem in formatting the decimal number, Suppose i can have two decimal number in which decimal can be before or after 18th digits, format like:

$num1 = 12345678912345678912345.52458

$num2 = 123.54893256666666

for above two number my desired out is base on condition that is

for $num1: if decimal comes after 18th digit so the decimal should be just after 18digit and after that 2 digit comes as a precision only

for $num2: if decimal comes before 18th digit so the decimal should be as it is position but the precision should be only 2 digit after decimal

desired out:

$num1 = 123456789123456789.00
$num2 = 123.54

I have tried this with number number_format() like below:

<?php
    $num1 = 12345678912345678912345.52458;
    $num2 = 123.54893256666666;
    $num1 = number_format($num1,2,'.',''); //it is not working
    $num2 = number_format($num2,2,'.',''); //it is working
    echo $num1."<br>".$num2;

output:

for $num1 = 12345678912345679593472.00 where it should comes like 123456789123456789.00

for $num1 = 123.55 that is ok 

please help i am not getting how to use number_format() for $num1

lazyCoder
  • 2,544
  • 3
  • 22
  • 41

1 Answers1

0

Why you need that large number? by default php supports 14 digits. for example

$num = 12345678122222;
if (strpos($num, '.') !== false) {
    echo 'true';
}else
    echo 'false';

If you write that code, it will echo false that is correct but if you add one more digit to $num then php will not handle it properly.

And for you two digit after decimal. See this link it is already answered

PHP: show a number to 2 decimal places

Muhammad Umair
  • 103
  • 1
  • 12