If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Decimal number not working

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-09-04, 10:22
massimoMXMUS massimoMXMUS is offline
Registered User
 
Join Date: Mar 2004
Location: London
Posts: 4
Lightbulb Decimal number not working

Hi fellas,

I have a problem storing a decimal number in a MyISAM database,

when I create a DECIMAL (10,2) field in a table I have the following issue:

if I store the value 1.59 I can view 1.59


if I store the value 1.50 mysql shows 1.5, what I am trying to achieve is to store 1.50

When PHP connect to the database and display the numbers on the page the decimal position are not correct, 1.5 is not 1.50

Any idea?

Many many thanks for your help
Reply With Quote
  #2 (permalink)  
Old 03-09-04, 10:35
bstjean bstjean is offline
Registered User
 
Join Date: Sep 2002
Location: Montreal, Canada
Posts: 219
Re: Decimal number not working

Quote:
Originally posted by massimoMXMUS
Hi fellas,

I have a problem storing a decimal number in a MyISAM database,

when I create a DECIMAL (10,2) field in a table I have the following issue:

if I store the value 1.59 I can view 1.59


if I store the value 1.50 mysql shows 1.5, what I am trying to achieve is to store 1.50

When PHP connect to the database and display the numbers on the page the decimal position are not correct, 1.5 is not 1.50

Any idea?

Many many thanks for your help
Well, I created a test database with 1.5, 1.50 and a few other values (DECIMAL (10, 2)) and it displays properly when queried from the MySQL client. Looks like you problem is somewhere else... Have you tried selecting those values directly from the MySQL client?
Reply With Quote
  #3 (permalink)  
Old 03-09-04, 10:41
massimoMXMUS massimoMXMUS is offline
Registered User
 
Join Date: Mar 2004
Location: London
Posts: 4
decimal (10,2)

Hi bstjean,

thanks for helping me,

I use mysql front 2.5 as the client interface and when insert some data in, eg. 1.50 and I change the mouse to the next record, it updates the record and all it display is 1.5

when I use php to retreive the data it also display 1.5 and I can't figure out why it is doin this.

Which client do you use to connect to mysql db?

thanks
Reply With Quote
  #4 (permalink)  
Old 03-09-04, 10:50
bstjean bstjean is offline
Registered User
 
Join Date: Sep 2002
Location: Montreal, Canada
Posts: 219
Re: decimal (10,2)

Quote:
Originally posted by massimoMXMUS
Hi bstjean,

thanks for helping me,

I use mysql front 2.5 as the client interface and when insert some data in, eg. 1.50 and I change the mouse to the next record, it updates the record and all it display is 1.5

when I use php to retreive the data it also display 1.5 and I can't figure out why it is doin this.

Which client do you use to connect to mysql db?

thanks
Mysql.exe on Windows (should be mysql) on Unix...

Like:

mysql --user=yourusername --password=yourpasssword databasename

Then:
Use yourdatabasename;

Then:
SELECT * from yourtable;
Reply With Quote
  #5 (permalink)  
Old 03-10-04, 05:21
massimoMXMUS massimoMXMUS is offline
Registered User
 
Join Date: Mar 2004
Location: London
Posts: 4
Talking It was so easy, using php

Hi Everybody,

I think this is a useful piece of code and all of you will need it

My problem was this:

After retrieving a number field from a mysql db like 1.50 or 1.60 and multipling this number * 1 the result I was getting was 1.5 or 1.6

In fact if you also use a calculator and put 1.50 * 1 it gives you 1.5... where does the second decimal number go?

echo "$data->price"; // 1.50
$quantity_in_chart = 1;
$variable = ($data->price * $quantity_in_chart); // 1.5
$variable = sprintf("%.2f",$variable); // 1.50

I was pulling my hair out before founding this.

Hope this will help some other PHP/MySQL buddies
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On