-2

Is this the right way to show the float number within the MySQL Database?

if ($a == "balance") {
    $querys= "SELECT balance FROM users WHERE Username='$user'";
    $results=  mysql_query($querys);
    $rows = mysql_fetch_row($results);
    $bfloat = (float)$rows['balance']; 
    echo $bfloat;
}

in the MySQL database the column name balanace is float and as example at the current user the value is 1.73. So its answer would be 1.73

D.M.
  • 23
  • 1
  • 1
  • 5
  • 1
    Don't use floats to work with monetary values! http://floating-point-gui.de – deceze Jun 30 '14 at 08:14
  • 1
    Well, the `mysql` extension is deprecated, and you should do something about that. Other than that, what exactly is your question? [This question](https://stackoverflow.com/questions/2251290/storing-money-amounts-in-mysql), or [this one](https://stackoverflow.com/questions/13030368/best-data-type-to-store-money-values-in-mysql), may be relevant here. – Sverri M. Olsen Jun 30 '14 at 08:14
  • @SverriM.Olsen i try to retrieve the stored amount in the database that has the type `float` and as example the float number is `1.73` – D.M. Jun 30 '14 at 09:37

1 Answers1

0

Bad news. The answer should be approximately 1.73. For that in typed languages separate decimal types are used (java BigDecimal). See Decimal type in php for the same in PHP.

Floating point is approximates our decimal representation, it would need infinite number of digits in the binary system (base 2). Just as 2/7 in base 10, whereas it is 0.2 in base 7.

Also it is better tp accustom oneself to use prepared statements http://xkcd.com/327/

Community
  • 1
  • 1
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • "In typed languages"...?! As opposed to PHP, which is type-free...?! – deceze Jun 30 '14 at 08:23
  • @deceze Well, PHP is weakly typed; one type can often be used as another type. He probably meant "strongly typed languages". – Sverri M. Olsen Jun 30 '14 at 09:48
  • @Sverri "Weak" typing isn't actually a thing. [What to know before debating type systems](http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html) – deceze Jun 30 '14 at 09:50
  • _Ah, I have a programming language background, so my wording on typing was bad wording. Not long ago our Pretty Horrible Programming language even got a better performance due to types._ My point: in PHP types mainly occur as afterthought, _cast_. The language certainly does not give fixed point numbers the needed attention: tutorials, best practices, professional norms. (Okay java's BigDecimal is ugly too.) – Joop Eggen Jun 30 '14 at 11:41
  • @deceze Well, you know what I am talking about. That makes it "a thing". – Sverri M. Olsen Jun 30 '14 at 11:57
  • @Joop You're still conflating the presence or absence of types with dynamic typing vs. static typing. PHP always had types. It's simply that PHP's type system dynamically converts between types based on context and infers types from context. As opposed to explicitly, statically typed languages like Java. That doesn't mean Java *has* types and PHP *doesn't* or that PHP's type system is "an afterthought". True though that PHP doesn't happen to have a specific type for decimal numbers. – deceze Jun 30 '14 at 12:05
  • @Sverri The words you're looking for are "dynamic" vs. "static". :) – deceze Jun 30 '14 at 12:09
  • I just realised it's still possible to easily start a discussion by bringing up type systems on the interweb. I'll leave this be for now... ;) – deceze Jun 30 '14 at 12:10
  • @deceze I appreciate a bit of tech talk, your remarks which are to the point. But I am afraid, the question was for 2-decimals format function. And now marked as duplicate. – Joop Eggen Jun 30 '14 at 13:32