-2

I want to time a decimal result in sql, like this :

// script 
$currency =mysqli_fetch_array( $db->query("SELECT value FROM `currency` where `currency_code` = 'USD' ") );
// $currency['value'] = 0.00001234;
// type : float(15,8)
$result = 5000 * $currency['value'] ;
// end script

and the result is 0.

I want a real result.. What must I change ?

halfer
  • 19,824
  • 17
  • 99
  • 186
bobbi
  • 1
  • 1
  • float with currency? I suggest you re-examine your choice of data type. http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – xQbert Sep 15 '15 at 12:46
  • I very much doubt that the values and value types are what you say they are. You should start with a `var_dump($currency);`. – jeroen Sep 15 '15 at 12:48

2 Answers2

1

I suggest you to type cast your result value into float first and then make the multiplication.

 $currency =mysqli_fetch_array( $db->query("SELECT value FROM `currency` where `currency_code` = 'USD' ") );

 // $currency['value'] = 0.00001234;
 // type : float(15,8)
 $result = 5000 * (float)$currency['value'];
 // end script

Give it a try and let me know the result please.

CntkCtn
  • 357
  • 2
  • 7
  • can you give me that code? I dont know what must I change. please. – bobbi Sep 16 '15 at 03:28
  • Just add (float) infront of $currency['value']; I already gave it in the answer. $result = 5000 * (float)$currency['value']; – CntkCtn Sep 16 '15 at 08:25
0

You can do it directly in query like this:

$currency =mysqli_fetch_array( $db->query("SELECT (value * 5000) as totalVal FROM `currency` where `currency_code` = 'USD' ") );

And you can use "totalVal" directly to access the value.

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90