tags:

views:

64

answers:

5

Hi this is my array

Array ( [0] => Array ( [0] => 21.2694 USD ) [1] => Array ( [0] => 21.2694 USD ) )

(this is basicall google currency converter)

echo $arr[0][0]; //output is 21.2694 usd

But i want Output like 21.26 USD

What is tried is , echo substr($arr[0][0],0,5);//For this substr line i expected output is 21.26

But am getting just blank page,

Where i made mistake

i want 21.26USD as the output if i give the input as 21.2694 USD

Basically am doing the currency converter (google api)

But api producing result as 29.5645 USD from in this , i want to make out put as 29.56 USD ,

This is my snippet ,

   
    $from_currency = "GBP";
    $selected_currency = 'USD';
    $txt_cash_price = '10';
    $txt_card_price = '14';

    $get_currency = file_get_contents("http://www.google.com/finance/converter?a=$txt_cash_price&from=$from_currency&to=$selected_currency");
    preg_match_all("/(.*)/", $get_currency, $matches,PREG_PATTERN_ORDER);
    $caprice = $matches[0][0];

       OUPUT SOMTHING LIKE THIS 15.2070 USD

But i want output as 15.20 USD

+3  A: 

You should use number_format() (documentation):

string number_format ( float $number [, int $decimals ] )

So here is an example:

echo number_format((float)$arr[0][0], 2), " USD";

What happens here is that we get the right value from the array, and cast it to float. That makes PHP remove the text part of the value, so you are left with only the number. Then number_format formats it to include 2 decimals, and finally we echo out " USD" afterwards.

Vegard Larsen
you should not let number_format deal with removing the text as its designed to take strings in the parameters and does not have a set type, it will cause more memory than needed.
RobertPitt
A: 

Don't use substr() for this. Use number_format().

echo number_format($arr[0][0], 2);

however, the way you do it should work in principle, too (even though it's not the best way). Make a test output of $arr[0][0] to see whether it contains any value.

Unicron
echo number_format($caprice, 2); Got output as 0.00
Bharanikumar
@Bharani ah, you need to remove the "USD". Try `rtrim($caprice, "a..z ");` or use `(float)` as @Vegard recommends.
Unicron
A: 

Well you need to make the value of the array a float instance so do this

$price = (float) $arr[0][0]; //This will covert it to pure float number.

Then you can use number_format to get the decimal places just right.

Heres my test case:

$myArray = array(
   array(
      '21.34957 USD.'
   )
);

$float = (float)$myArray[0][0]; //21.34957 | removes the text and turns to a float.

echo number_format($float, 2); //21.35
RobertPitt
i cant aboe to made any chanes in that array value,If i cant able to remove the USD means i simply use the substr,But i dont know why, i cant able to make any changes in the array value,
Bharanikumar
Cna you please make your comment readable. "If i cant able to remove", "i cant aboe to made any chanes", " cant able to make" ??
RobertPitt
+1  A: 

You can use round

$str = '15.2070 USD';
echo round($str,2)." USD"; //15.21 USD
SpawnCxy
A: 

If you want just the plain number, with none of the extra formatting that number_format() adds, you can use sprintf():

 echo sprintf('%0.2f', 21.2694);    -> 21.26
Marc B