user3162341
user3162341

Reputation: 251

number is missing decimal

I have some numbers in PHP : 19,6, 33,33, 5,5

I want to get 1,196, 1,333 and 1,055.

(19.6 / 100) + 1 = 1.196

(33.33 / 100) + 1 = 1.333

(5.5 / 100) + 1 = 1.055

I have this value,

$item['tva'] = 1 + ($tvas[$tmpItem] / 100);

And in JSON I'm getting 1.19 not 1.196 why ?

Upvotes: 0

Views: 100

Answers (1)

dersvenhesse
dersvenhesse

Reputation: 6424

I made an working example.

// array with numbers to add
$numbers = array(19.6, 33.33, 5.5);

// array to put formatted numbers in
$result = null;

foreach ($numbers as $i => $number) {
     // format number to have 3 decimal values and push to result array
     $result[$i] = number_format(1 + ($number / 100.), 3);
}

// decode array to json and force keys
print json_encode($result, JSON_FORCE_OBJECT);

This gives:

{"0":"1.196","1":"1.333","2":"1.055"}

Upvotes: 1

Related Questions