iwilson Posted June 29, 2011 Share Posted June 29, 2011 I set up a working timesheet logger for employees at my company through PHP and stored into mysql. I changed them storing from integers to decimals (also tried doubles and float with same results) with a length of "2,1" which should let them two digits with a decimal (a half hour)... This seems to work, but I've just noticed now when storing double digits (10, 20) SQL always saves it as 9.9I know my PHP form is sending the numbers correctly, I've tested with echoing them back right before the update query and they show up as the whole numbers they are... Any clues? Thanks. Link to comment Share on other sites More sharing options...
[dx] Posted June 29, 2011 Share Posted June 29, 2011 I had similar problem when working with money.Then I've tried with PHP's number_format() function, but for some reason it won't work for me, so I wrote function which does what I want. // decimal_num(15.7545, 2) -> output: 15.75function decimal_num($n, $d) { global $add_zero; $add_zero = ''; if (($n) && ($d)) { if (substr($n, (strlen($n)-1), 1) == '.') $n = substr($n, 0, -1); if (substr($n, 0, 1) == '.') $n = '0.' . substr($n, 1, $d); if (count(explode('.', str_replace(',', '.', $n))) > 2) { $n = explode('.', str_replace(',', '.', $n)); $n = $n[0] . '.' . $n[1]; } if (count(explode('.', str_replace(',', '.', $n))) == 2) { $n = explode('.', str_replace(',', '.', $n)); if ($d > strlen($n[1])) for ($i = (strlen($n[1])+1); $i <= $d; $i++) $add_zero .= '0'; else $n[1] = substr($n[1], 0, $d); return $n[0] . '.' . $n[1] . '' . $add_zero; } if (count($n) == 1) { if ($d > 0) $add_zero = '.'; for ($i = 1; $i <= $d; $i++) $add_zero .= '0'; return $n . $add_zero; } } else if ($n == 0) { if ($d > 0) { $add_zero = '.'; for ($i = 1; $i <= $d; $i++) $add_zero .= '0'; } return '0' . $add_zero; } else return 'Parameters missing in function decimal_num()';} Filter your numbers through this function and use VARCHAR for that column in SQL.No matter if you write 10,2 or 10.2, it'll save it as 10.2 Link to comment Share on other sites More sharing options...
justsomeguy Posted June 30, 2011 Share Posted June 30, 2011 When you use a length of 2,1, you're telling it to store a maximum of 2 digits, with a maximum of 1 digit after the decimal point. The 2 is the total number of digits, not the number of digits left of the decimal. Link to comment Share on other sites More sharing options...
iwilson Posted July 4, 2011 Author Share Posted July 4, 2011 Thanks for the replies... Changing the length to 3,1 fixed that issue. Another issue I have now, is that all the numbers return with a .0 if they are whole... So reading the timesheets afterwards is a slight headache, I thought I could fix this with: if($d[$count]=="0.0") $d[$count]=0; elseif(substr($d[$count],1,2)==".0") $d[$count]=substr($d[$count],0,1); elseif(substr($d[$count],2,2)==".0") $d[$count]=substr($d[$count],0,2);$d array being where the hours stored, 0-6 (mon-sun). However, 10.0 passed through this comes out as 1... I guess substr doesn't like numbers.$d[$count]=trim($d[$count],".0");Also produces the same results Link to comment Share on other sites More sharing options...
iwilson Posted July 4, 2011 Author Share Posted July 4, 2011 2 minutes later and I have fixed it...$d[$count] = trim(trim($d[$count], '0'), '.');Works fine, incase anyone has a similar issue. Thanks again for the replies! Link to comment Share on other sites More sharing options...
justsomeguy Posted July 5, 2011 Share Posted July 5, 2011 With regard to substr, you can do this to get the last 2 characters:substr($str, -2) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.