polardude1983 Posted February 12, 2009 Share Posted February 12, 2009 I have a current_weight field. where ppl have their weight put in there so. if someone enters their weight in the form as 228 then the new_weight field says 22 This works if i keep it as new_weight CHAR(2), but then the 22 is a character and cannot be added to anything else since it is not an integer so how can i do it so where new_weight is an integer but only grabs the first 2 numbers from 228? new_weight INT(2) does not work it still grabs all 3 any idea on how to do this? im very puzzled Quote Link to comment https://forums.phpfreaks.com/topic/144858-solved-unfort-gonna-have-to-reopen-this-get-first-to-numbers-but-keep-as-int/ Share on other sites More sharing options...
trq Posted February 12, 2009 Share Posted February 12, 2009 if someone enters their weight in the form as 228 then the new_weight field says 22 Why if a user enters 228 are you trying to store it as 22? 228 is a long way from 22. Quote Link to comment https://forums.phpfreaks.com/topic/144858-solved-unfort-gonna-have-to-reopen-this-get-first-to-numbers-but-keep-as-int/#findComment-760140 Share on other sites More sharing options...
polardude1983 Posted February 12, 2009 Author Share Posted February 12, 2009 well I'm wanting to keep the original 228 number as it is used later. but i want to extract the first 2 numbers from that 3 digit number and still keep it as an integer. if someone enters 128 ill use that number later, but when i extract the first 2 numbers i will get 12. and i will need to use that new number later too. Quote Link to comment https://forums.phpfreaks.com/topic/144858-solved-unfort-gonna-have-to-reopen-this-get-first-to-numbers-but-keep-as-int/#findComment-760145 Share on other sites More sharing options...
corbin Posted February 12, 2009 Share Posted February 12, 2009 weight / 10 will give you the first 2 digits. Note though that if someone puts in a weight under 100, it will give you 1 digit. Also, you may want to floor() the number since MySQL will throw a warning if you try to store a float in an integer field. Quote Link to comment https://forums.phpfreaks.com/topic/144858-solved-unfort-gonna-have-to-reopen-this-get-first-to-numbers-but-keep-as-int/#findComment-760146 Share on other sites More sharing options...
polardude1983 Posted February 12, 2009 Author Share Posted February 12, 2009 of course thats right weight / 10 duh so if they enter 228. it stores it as 22.8 which it then actually rounds it up to 23. which is ok i guess kinda throws things off. i would actually like it to always round down and never up. thanx for your help so far Quote Link to comment https://forums.phpfreaks.com/topic/144858-solved-unfort-gonna-have-to-reopen-this-get-first-to-numbers-but-keep-as-int/#findComment-760171 Share on other sites More sharing options...
corbin Posted February 12, 2009 Share Posted February 12, 2009 Casting to int will round down, as will floor(). Quote Link to comment https://forums.phpfreaks.com/topic/144858-solved-unfort-gonna-have-to-reopen-this-get-first-to-numbers-but-keep-as-int/#findComment-760192 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.