Jump to content

Double digits change to 9.9 ...


iwilson
 Share

Recommended Posts

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...