Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sprintf() and %d 1

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
Can someone help with using %d in a sprintf()? I thought it would take the signed integers which, in this case, are for latitude and longitude, and insert them and it does do so but it rounds them to all zeros after the decimal. The MySQL fields are decimal(8,6) and decimal(9,6) respectively so what would be the proper way to do it other than %s? %s, of course, works but does not seem the best way.

PHP:
$sqlInsert = sprintf("INSERT INTO locations (StartDate, EndDate, Location, Address, Lat, Lng) 
	 VALUES ('%u', '%u', '%s', '%s', %d, %d)",
		  $StartDate,
		  $EndDate,
		  $Location,
		  $Address,
		  $Lat,
		  $Lng);
8
 
There's %f for decimals or floats.

You can specify the number of decimals before the f.
Code:
$sqlInsert = sprintf("INSERT INTO locations (StartDate, EndDate, Location, Address, Lat, Lng) 
	 VALUES ('%u', '%u', '%s', '%s', [COLOR=#CC0000][b]%0.2f, %0.2f[/b][/color])",
		  $StartDate,
		  $EndDate,
		  $Location,
		  $Address,
		  $Lat,
		  $Lng);


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Great, thank you! That did the trick. I couldn't make sense of the php.net manual's entry, which I had already reviewed before posting here, but your example made perfect sense.
 
You're most welcome.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top