Jump to content

[SOLVED] Losings decimals from array


janders

Recommended Posts

Hey Everyone,

 

I'm new to this form but have a good amount of PHP knowledge.

 

I am trying to do something really simple but running into a problem:

 

I have an array that looks like this:

Array ( [0] => 6.99 [rate] => 6.99 [1] => 6.89033 [semi_annual_rate] => 6.89033 )

 

I do the following to get values:

$rate=$rate['rate'];

$semi_annual_rate=$rate['semi_annual_rate'];

 

echo $rate 6.99; // Returns 6.99 which is correct

echo $semi_annual_rate; // Returns 6 when it should be returning 6.89033

 

Does anyone know why I can't get $semi_annual_rate to return 6.89033?

 

I tried number format and a couple other functions but nothing seemed to work.

 

Any help would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/103264-solved-losings-decimals-from-array/
Share on other sites

You are reusing the variable $rate after you define it as an array. Use a different variable:

<?php
$rate = Array ( [0] => 6.99 [rate] => 6.99 [1] => 6.89033 [semi_annual_rate] => 6.89033 );
$rate1=$rate['rate'];
$semi_annual_rate=$rate['semi_annual_rate'];
echo $rate1 . '<br>';
echo $semi_annual_rate;
?>

 

But, why use another variable at all when you can just reference the array index.

<?php
echo $rate['rate'] . '<br>';
echo $rate['semi_annual_rate'];
?>

 

Ken

Thanks everyone for your help!

 

I've never used forms for help before but I like it already!

 

It ended up being a stupid mistake pointed out by kenrbnsn where I was reusing the variable $rate.

 

Thanks also to jonsjava for your input, what you suggested would work if I wasn't reusing that variable.

 

Thanks again and have a good one!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.