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 > float decimal problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-09-09, 03:42
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
float decimal problem

hi,
i cant figure out why my field `AmountFigure` is keep on rounding off
i have a value of 52206.25
and when i insert it in my table.AmountFigure it became 52206.3!
am i missing anything here?

please kindly help..
thanks
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #2 (permalink)  
Old 03-09-09, 05:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by homer.favenir
am i missing anything here?
yes, you've forgotten to give us all the facts about the column
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-09-09, 06:15
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
Code:
CREATE TABLE  `mafee_db`.`banktransact_tbl` (
  `idbank_transact` int(10) unsigned NOT NULL auto_increment,
  `Date` date NOT NULL default '0000-00-00',
  `AccountNumber` varchar(45) NOT NULL default '',
  `AccountName` varchar(100) NOT NULL default '',
  `Currency` varchar(45) NOT NULL default '',
  `AmountWords` text NOT NULL,
  `TransactType` varchar(45) NOT NULL default '',
  `TransactBy` varchar(45) NOT NULL default '',
  `PreparedBy` varchar(45) NOT NULL default '',
  `Bank` varchar(100) NOT NULL default '',
  `Branch` varchar(45) NOT NULL default '',
  `Note` text,
  `AmountFigure` float NOT NULL default '0',
  PRIMARY KEY  (`idbank_transact`),
  KEY `Index_2` (`Date`,`AccountNumber`,`TransactType`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
the field is `AmountFigures` it suppose to accept Float,
but when i insert 52206.25, it became 52206.3

thanks
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #4 (permalink)  
Old 03-09-09, 06:25
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
I would have thought for a n umneric, especially a monetary value you would be using a fixed precision value such as decimal(size,NoDecimals). although you cna use a FLOAT/DOUBLE in MySQL using and sepcify a size and precision of value

MySQL :: MySQL 5.0 Reference Manual :: 10.2 Numeric Types

persoanlly I would expect to see soemhtign such as decimal(16,4), that would allow 12 places to the left of the decimal and 4 to the right). you oculd vary that to meet your requirements (ie if you know you will only ever need 2 decimal places and you are only going to have say 5 to the left (ie range -9,999.99 to 99,999.99 then you could specify decimal (7,2). but for most currency values you'd be better off establishing your maximum size and at least doubling it to allow for rounding and other errors (unless of course you are writing stuff for Zimbabwe* where you'd need to use a column as big as physically possible to allow for the rampant inflation)


*an other countries such as the UK and our soon to be inflation busting 'quantative easing' AKA printing money
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 03-09-09, 08:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
homer, try this and please show me what results you get --
Code:
CREATE TABLE  test_homer (
  `idbank_transact` int(10) unsigned NOT NULL auto_increment,
  `AmountFigure` float NOT NULL default '0',
  PRIMARY KEY  (`idbank_transact`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

INSERT INTO test_homer (`AmountFigure`) VALUES
 ( 9.37 )
,( 9.037 )
,( 9.0037 )
,( 9.00037 )
,( 9.000037 )
,( 9.0000037 )
,(      93.7 )
,(     930.7 )
,(    9300.7 )
,(   93000.7 )
,(  930000.7 )
,( 9300000.7 )
;
SELECT * FROM test_homer 
;
please see if you can offer an explanation for what's happening

see if the phrase "number of significant digits" might mean anything in this context

now realize that every number you insert into a FLOAT column is converted first to scientific notation --
x.xxxxx * 10^^(+/-nnn)
the number x.xxxxx will have only 1 digit to the left of the decimal point, and the number nnn will indicate the power of 10 to be applied

the important point here is that x.xxxxx has only a certain number of significant digits

you did not specify how many, so it used the default

any idea what that default might be?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 03-09-09, 22:07
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
@r937
this is exactly what i need.
but im wondering, what is the difference between my table and your table?
do i have to drop my previous table and make a new one?
thanks
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #7 (permalink)  
Old 03-09-09, 22:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
i think you can simply modify the table

try this (untested) --

ALTER TABLE mafee_db.banktransact_tbl
CHANGE COLUMN AmountFigure DECIMAL(9,2) NOT NULL DEFAULT 0.0

make sure you choose an adequate scale and precision for your DECIMAL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 03-09-09, 22:31
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
however it doesnt work
error:
Code:
ALTER TABLE mafee_db.banktransact_tbl 
CHANGE COLUMN AmountFigure DECIMAL(9,2) NOT NULL DEFAULT 0.0
Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECIMAL(9,2) NOT NULL DEFAULT 0.0' at line 2
and also the
Code:
CREATE TABLE  test_homer (
  `idbank_transact` int(10) unsigned NOT NULL auto_increment,
  `AmountFigure` float NOT NULL default '0',
  PRIMARY KEY  (`idbank_transact`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

INSERT INTO test_homer (`AmountFigure`) VALUES
 ( 9.37 )
,( 9.037 )
,( 9.0037 )
,( 9.00037 )
,( 9.000037 )
,( 9.0000037 )
,(      93.7 )
,(     930.7 )
,(    9300.7 )
,(   93000.7 )
,(  930000.7 )
,( 9300000.7 )
;
SELECT * FROM test_homer 
;
when i insert the value 52206.75 in 'AmountFigure' it became 52206.8
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;

Last edited by homer.favenir; 03-09-09 at 22:36.
Reply With Quote
  #9 (permalink)  
Old 03-09-09, 22:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by homer.favenir
however it doesnt work
you need to look it up in the manual yourself

i think the syntax requires the old column name and then the new column name

but ~you~ should be looking it up and testing it
Code:
ALTER TABLE mafee_db.banktransact_tbl 
CHANGE COLUMN AmountFigure 
AmountFigure DECIMAL(9,2) NOT NULL DEFAULT 0.0


Quote:
Originally Posted by homer.favenir
when i insert the value 52206.75 in 'AmountFigure' it became 52206.8
yes, that's right

that's why i asked you to do it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 03-09-09, 23:22
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
Quote:
Originally Posted by r937
you need to look it up in the manual yourself

i think the syntax requires the old column name and then the new column name

but ~you~ should be looking it up and testing it
Code:
ALTER TABLE mafee_db.banktransact_tbl 
CHANGE COLUMN AmountFigure 
AmountFigure DECIMAL(9,2) NOT NULL DEFAULT 0.0


yes, that's right

that's why i asked you to do it
DECIMAL(9,2) doesnt work also
it rounds off my 'AmountFigures' in one decimal place.
i need this 'AmountFigures' to be round off in three decimal places.

please help

thanks
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #11 (permalink)  
Old 03-09-09, 23:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by homer.favenir
i need this 'AmountFigures' to be round off in three decimal places.
OH FOR CRYING OUT LOUD!!!

did you not understand what i meant when i said that you should choose an adequate scale and precision for your DECIMAL

obviously, the "2" in DECIMAL(9,2) is inadequate, yes?

i will not answer any more in this thread for a minimum of 24 hours, to give you a chance to test these changes for yourself

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 03-09-09, 23:49
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
ok, i got it. thanks rudy...
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #13 (permalink)  
Old 03-10-09, 05:04
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Kill Nothing But Time;
How apt
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