/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] php eqautions

Greetings so hear is my delema have a page that uses following

<?php echo number_format($data[‘value’]*0.0321507466*5*0.35, 2);?> where $data[‘value’] = 19.54 and outputs the result as 1.10 which is correct.

how ever if i change it to say

<?php echo number_format($data2[‘value’]*0.0321507466*1.672*0.9, 2);?> and $data2[‘value’] = 1,291.30 the output is displaying 0.05 when it should be reading 62.47

not sure why this is happening anyone have a idea as to why this would be? should the $data2[‘value’] = 1,291.30 not have a ‘ , ‘ in it and if so any clue how to get rid of it?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 25.2014 — Yes, the comma will cause a problem, as PHP numeric types to not any commas. In this case, it's considered a string, and when it tries to type-cast it to a number, it only reads the first character (the "1"), then stops when it reaches anything that is not a number of a decimal point (the comma).

You could get rid of any commas with a simple [url=http://php.net/str_replace]str_replace()[/url] call.
Copy linkTweet thisAlerts:
@adistrolaauthorApr 25.2014 — Thanks so how would i work it into the code above. should i put it in the file that contains the code for $data['value']? just googled the function dont understand how it works.
Copy linkTweet thisAlerts:
@Error404Apr 25.2014 — You could do something like this just before you perform your calculations:

[code=php]
if(strpos($data2['value'], ",") !== false) {
$data2['value'] = (float) str_replace(",", "", $data2['value']);
}
[/code]


In words, it checks whether there is a comma anywhere in $data2['value'] and if so, it replaces it with "" (nothing). Since it's still technically a String, the result is then explictly casted to a float so you can use it for calculations. PHP probably will implicitly cast it for you but I like being specifying it.
Copy linkTweet thisAlerts:
@NogDogApr 25.2014 — If you don't want to mess with the source data and may need to use the "fixed" version in more than one place, you could create a separate variable:
[code=php]
$data2Float = (float) str_replace(',', '', $data2['value']);
[/code]
...then use $data2Float in your calculation instead of $data2['value'].

Alternatively, if that one calculation is the only place you use it, I'd probably just stick it in-line:
[code=php]
<?php
echo number_format(((float) str_replace(',', '', $data['value'])) * 0.0321507466 * 5 * 0.35, 2);
?>
[/code]
×

Success!

Help @adistrola spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...