convert a scientific notation/exponential number [duplicate] - php

This question already has answers here:
Convert exponential number presented as string to a decimal
(3 answers)
Closed 5 years ago.
I am trying to convert a scientific notation/exponential number.
For example :
I get from a API request this number : 4.96E-6
I want to convert it to his normal value 0.00000496.
I searched on the web and tryed a few codes like
$awb = "4.96e-6"; // tried with and without quotes
$format = sprintf($awb, "%e");
var_dump($format);
it returns : string(7) "4.96E-6"
$awb = 4.96e-6;
$format = sscanf($awb, "%e");
var_dump($format);
Is returning :
array(1) { [0]=> float(4.96E-6) }
Please what should I do ?
Thank you

You can use the number_format() function; e.g.
<?php
$awb = "4.96e-6";
print number_format($awb, 10);
?>
Will result in the output: 0.0000049600.
See: http://php.net/manual/en/function.number-format.php

Related

How to change all front decimal to 0 in php [duplicate]

This question already has answers here:
Add zero to before decimal point of a number
(2 answers)
Closed 2 years ago.
I have some data from database like this :
43966.31875
how to change 43966 to 0 and keep decimal number to 31875, What should I do?
well, for the final result like this :
0.31875
If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You
The regex method:
$string = '43966.31875';
echo preg_replace('/(\d+).(\d+)/i', '0.${2}', $string);
Will split number into 2 parts then replace the part 1 with "0."
The strstr method:
$string = '43966.31875';
echo '0'.strstr($string, '.');
Also split the number into 2 parts and take the second part and add "0" before
$number = 43966.31875;
$decimalPart = fmod($number, 1);
$decimalCount = explode('.', $number)[1];
$result = round($decimalPart, strlen($decimalCount));
Refer the Official Documentation:
PHP fmod

PHP - PRINTF Not Printing array $variable[$i] [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 3 years ago.
I'm trying to printf a variable with some 000 on the left side but only print the 000 and no print the variable.
$activePlayers1 = array(3)
numero = $activePlayers1[$i];
printf('000',$numero);
The result of this is 000 and no print the 3.
If you are trying to pad with zeros, you should use a padding specifier:
<?php
$numero = 5;
printf("%'.09d", $numero);
?>
yields
000000005
Try it yourself
The d is a type specifier, used to treat the variable value as an integer and display it as a decimal, and you can find the full list of type specifiers under the sprintf documentation.
d - the argument is treated as an integer and presented as a (signed)
decimal number.
It should be like this
$activePlayers1 = array(3)
numero = $activePlayers1[$i];
printf('%d',$numero);

How to show only two numbers after decimal using php eval() or round()? [duplicate]

This question already has answers here:
How can I format a number into a currency value without php?
(3 answers)
Closed 4 years ago.
I am calculating result of my expression using eval() function but its showing all the numbers afer decimal and I want to show only two numbers after decimal
this is my eval function with other data that I have stored.
$expression = generate_expression($num_operands,
$operations, $num_digits);
$expression_string = implode(" ", $expression);
$result = eval("return ($expression_string);");
$expressions[] = compact("expression", "result");
I am storing the expression_
for this I tried using round function also but its not showing the result that I want
here is the round function for the $result
<?php echo round($result,2); ?>
Where I went wrong?
Use: number_format()
<?php echo number_format((float)$result, 2); ?>
Example

reverse chunk split of integer in PHP [duplicate]

This question already has answers here:
Formatting numbers with commas PHP [duplicate]
(4 answers)
Closed 7 months ago.
I need split by gap like when we write count numbers we will give gap for every 3 integer.
echo chunk_split(1000255869,3," ");
I got output like: 100 025 586 9
But I need output like: 1,000,255,869
How to reverse that one?
Try this,
<?php
$a = 1000255869;
echo $a;//output - 1000255869
$a = number_format($a);
echo $a;//output - 1,000,255,869
?>
if you want to do a number format, you should do it with number_format.
echo number_format(1000255869,0,".",",");
This should work number_format(1000255869, 0, '.', ',');
Try this.Reference
$number= 1000255869;
echo number_format($number);

Converting number-formatted variable back to original format - PHP [duplicate]

This question already has answers here:
How do I convert output of number_format back to numbers in PHP?
(12 answers)
Closed 1 year ago.
I used number_format in a number. I want to know how to convert number-formatted variable in php back to its original format
I first used number_format. And I want to echo back its original format removing the number format.
$number = 500000
echo number_format($number, 2);
$number = 500000;
$formattedNumber = number_format($number, 2);
$getBackOriginl = explode('.',$formattedNumber);
echo str_replace(',','',$getBackOriginl[0]);

Categories