-1

I have this number: 1.8112336829e+01

I get it from xml file with function simplexml_load

$xml = simplexml_load_file("data.xml");
$number = $xml->data->row->v[1]; // assign number with the value 1.8112336829e+01
echo $number * 8; // outputs 1.

I want to multiply this by 8. On calculator: 1.8112336829 * 10^1 * 8 = 144.898694632

How can I format this the correct way with PHP to receive the right answer?

5less
  • 902
  • 1
  • 9
  • 18

1 Answers1

1

This works just like you said...

$number = 1.8112336829e+01;
echo $number * 8;

Edit

Maybe $number is an Object. Try to cast.

$xml = new SimpleXMLElement('<root><data><row><v>2</v><v>1.8112336829e+01</v></row></data></root>');

$number = $xml->data->row->v[1];
echo $number * 8; // outputs 8
echo (float)$number * 8; // outputs 144.898694632
Berriel
  • 12,659
  • 4
  • 43
  • 67
  • This outputs 8. PHP only multiplying (1*8). – 5less Aug 22 '15 at 15:05
  • Maybe you want to share your code and we can see what you are doing wrong – Berriel Aug 22 '15 at 17:17
  • Sorry Berriel, I should have tested your code. This is what I do: $xml = simplexml_load_file("data.xml"); $number = $xml->data->row->v[1]; echo $number * 8; From data.xml I get value 1.8112336829e+01 with $xml->data->row->v[1] where v[1] is the value. It seems this is not working when I get the value from xml? – 5less Aug 23 '15 at 10:07
  • See my edit. If it doesn't work, show the `var_dump($number)` – Berriel Aug 23 '15 at 12:54
  • Thats it! It was an Object. (float) was the way to go, thanks Berriel! – 5less Aug 23 '15 at 14:06