[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

converting SimpleXML string to decimal number

Posted on 2009-04-09
4
Medium Priority
?
664 Views
Last Modified: 2013-12-12
I am having an issue in regard to converting a string ("1.9") to a decimal value when SimpleXML is used.

Why does the code below round the value down to 1, and how can i store the full decimal value of the number?
$xmlstr = <<<XML
<numbers>
	<number>
		<value>1.9</value>
	</number>
</numbers>
XML;
 
$xml = new SimpleXMLElement($xmlstr);
$total=0;
foreach ($xml->number as $number) {
	echo '<p>'.$number->value.'</p>'; // 1.9
	$total+=$number->value;
}
echo '<p>'.$total.'</p>'; // 1

Open in new window

0
Comment
Question by:astrotoggle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 2000 total points
ID: 24106083
Just use a "cast" to indicate to PHP that you want it handled as a double.

$total += (double) $number->value

Amended code below
<?php
 
$xmlstr = <<<XML
<numbers>
        <number>
                <value>1.9</value>
        </number>
</numbers>
XML;
 
$xml = new SimpleXMLElement($xmlstr);
$total=0;
foreach ($xml->number as $number) {
        echo '<p>'.$number->value.'</p>'; // 1.9
        $total+= (double) $number->value;
}
echo '<p>'.$total.'</p>'; // 1
 
?>

Open in new window

0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 24106168
An xml element is just an array, php does not know that it is a number or a string, it just has a pointer to a memory location. To make the element what you wish it to be you can cast it, as suggested by bportlock, or just parse it with double quotes.

$total += "$number->value";
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24106339
Casting or use of floatval should be preferred as it makes the programmer's intention explicit both to himself and anyone who has to maintain that code. If you think that it is silly to make things clear to yourself, try maintaining code you wrote 12 months ago ....

One other point, never allow systems to run to defaults or set default values - always set them yourself. That way if the default ever changes then your code does not behave unexpectedly. That is what happened here. PHP forced this to an integer, but in PHP6 (say) that behaviour could change. If you depended on this "feature" to truncate floating point strings into integers then upgrading to a new version could break your code. By forcing it with a cast you

- protect your code against environment differences
- document your code
- reduce scope for errors
0
 

Author Closing Comment

by:astrotoggle
ID: 31568466
easy as that.  thank you very much for your help.  that was starting to do my head in.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question