Nandini Posted July 6, 2011 Share Posted July 6, 2011 Hi all I have one database filed like price with DECIMAL(30,2). While inserting values, those values are rounding. I am inserting values through PHP Coding. For example, I am inserting 5.625 but it is inserting as 5.63 I want to insert value 5.625 as 5.62. I have taken datatype as DECIMAL(30,3) but no use. Here is my code <?php mysql_connect("localhost","root",""); mysql_select_db("sample"); echo "Product Volume: 3.75<br>"; echo "Amount: 1.5<br>"; $total=3.75*1.5; echo "Total: ".$total; $total_insert=mysql_query("insert into test (price) values ('".$total."')"); ?> Link to comment https://forums.phpfreaks.com/topic/241227-mysql-decimal-datatype-problem/ Share on other sites More sharing options...
lordshoa Posted July 6, 2011 Share Posted July 6, 2011 can you not trim the last char off ? Link to comment https://forums.phpfreaks.com/topic/241227-mysql-decimal-datatype-problem/#findComment-1239104 Share on other sites More sharing options...
Nandini Posted July 6, 2011 Author Share Posted July 6, 2011 I am not trimming any value. Ho can i do this. I am not well in php. Please Link to comment https://forums.phpfreaks.com/topic/241227-mysql-decimal-datatype-problem/#findComment-1239107 Share on other sites More sharing options...
lordshoa Posted July 6, 2011 Share Posted July 6, 2011 <?php mysql_connect("localhost","root",""); mysql_select_db("sample"); echo "Product Volume: 3.75<br>"; echo "Amount: 1.5<br>"; $total=3.75*1.5; $total = substr_replace($total ,"",-1); echo "Total: ".$total; $total_insert=mysql_query("insert into test (price) values ('".$total."')"); ?> Link to comment https://forums.phpfreaks.com/topic/241227-mysql-decimal-datatype-problem/#findComment-1239109 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2011 Share Posted July 6, 2011 If you define your column as DECIMAL(30,3) before you insert the data, the mysql part of this will work. Link to comment https://forums.phpfreaks.com/topic/241227-mysql-decimal-datatype-problem/#findComment-1239110 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 With math $num = 1234567.89114524; $num = bcdiv(floor(bcmul($num,100)),100,2); echo $num; and strings $num = 1234567.891151351; $num = sprintf( '%01.2f', $num ); echo $num; Link to comment https://forums.phpfreaks.com/topic/241227-mysql-decimal-datatype-problem/#findComment-1239131 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.