Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I prevent this operation from returning an answer in Scientific Notation?

Tags:

php

math

I've got a fairly simple formula, although it involves a large-ish number, which is basically this:

$one = 1300391053;   
$two = 0.768;

// $millitime = 1.30039116114E+12    
$millitime = ($one+$two)*1000;

I understand this is the technically correct answer but I'm expecting to get 1300391053768. The goal of this is to get the time in milliseconds. I could combine the two and remove the decimal although that feels a bit odd.

Is there a way to get this to store 'properly' ?

[ as as side note, it seems not all installations handle this the same. My local PHP install (v5.3 on MacOS) returns the sci notation, but I run the identical code writecodeonline.com and get what I'm expecting/wanting. ]

like image 860
Will Avatar asked Nov 26 '25 21:11

Will


2 Answers

try using php's number_format()

so:

$one = 1300391053;   
$two = 0.768;
$millitime = number_format(($one+$two)*1000);
like image 164
Naftali Avatar answered Nov 28 '25 10:11

Naftali


What is "storing properly"? If you use print_r($millitime); you'll get 1300391053768. The value is stored "properly", the only question is how you want the output formatted. You could use number_format or sprintf for it.

echo sprintf('%.0f', $millitime) . PHP_EOL; // 1300391053768
echo sprintf('%e',   $millitime) . PHP_EOL; // 1.300391e+12

echo number_format($millitime, 0, '.', '') . PHP_EOL; // 1300391053768
like image 34
Czechnology Avatar answered Nov 28 '25 09:11

Czechnology



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!