Hacker
Hacker

Reputation: 7906

division in php

 $voltage = '0.4000';
                    $newValue = str_replace('0.','',$voltage);
                    echo 'newvalue'.$newValue;
                    $newValue = $voltage/0.0125;
                    echo 'newvalue'.$newValue;

when i do like this i get 32 after division where as i should get 320000. any problem in wht i am doing ?

Upvotes: 0

Views: 152

Answers (4)

prodigitalson
prodigitalson

Reputation: 60413

If youre doing number opertations then use numeric functions and varibles not strings...

$voltage = (float) '0.4000'; //cast as a float, assuming this comes from user input as string
$newValue = $volatge*1000;
echo 'newvalue'.$newValue;

$newValue = $newValue/0.0125;
echo 'newvalue'.$newValue;

Upvotes: 3

Pentium10
Pentium10

Reputation: 207830

 $newValue =  $newValue/0.0125;

Upvotes: 1

Andreas Wong
Andreas Wong

Reputation: 60506

Err... I'm sure it's typo, but I think you meant

$newValue = $newValue / 0.0125

Upvotes: 1

stevendesu
stevendesu

Reputation: 16771

You said $newValue = $voltage/0.0125. You probably meant to say $newValue = $newValue/0.0125

You never changed $voltage so it's still 0.4000, not 4000.

Upvotes: 3

Related Questions