/    Sign up×
Community /Pin to ProfileBookmark

Decimal number is recognized as whole number

Hello

I have a small problem. I have an input box on one page that posts a number into an int table/row in a mysql database. But if i for instance enter 0,50 the number becomes 0 in the database.

I have tried with (int) before the variable, or intval/floatval etc, but i cannot seem to get the number correct. Please help

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@tirnaMar 16.2010 — My recommendation would be to do form data validation to make sure you have clean data before inserting or updating your database.

In reality, data validation should be done on at least the server side because client side validation can be bypassed by mischievous users.

For 0,50 what is the desired value you would want that to equate to.?
Copy linkTweet thisAlerts:
@ReisauthorMar 16.2010 — i want the exact number, if i enter 0,50 i want it to input 0,50 ?

It seems it may be some conflict with the datatypes from the $_POST array and the INT datatype in the database maybe? If so, how can i make a string value to an exact decimal number?
Copy linkTweet thisAlerts:
@tirnaMar 16.2010 — If using a MySQL database then the data type for the table column needs to be decimal or numeric - they're both the same.

As part of your validation, make sure 0,5 is either rejected or changed to 0.5
Copy linkTweet thisAlerts:
@ReisauthorMar 16.2010 — So should this be correct and working:

[CODE]$amount = (int) str_replace(',','.', $data['amount']);[/CODE]
Copy linkTweet thisAlerts:
@NogDogMar 16.2010 — The key here is that if the MySQL column is defined as a float or decimal type, you [i]must[/i] use the "." (period/full stop) character as the decimal separator, not the "," (comma) in your SQL. If you want to allow the user to input numbers using the comma, then you will need to convert it to a period before using it in your query.
Copy linkTweet thisAlerts:
@ReisauthorMar 16.2010 — I solved it, I changed the mySQL datatype to float and changed my code to
[CODE]$amount = floatval(str_replace(',','.', $data['amount']));[/CODE]
Copy linkTweet thisAlerts:
@NogDogMar 16.2010 — So should this be correct and working:

[CODE]$amount = (int) str_replace(',','.', $data['amount']);[/CODE][/QUOTE]


In cases where the user enters something like "1234,56" that should be fine. However, what if the user enters "1,234.56" or "12,345,678" or "12.345,6". (Thus the problem with different standards around the world. :rolleyes: )
Copy linkTweet thisAlerts:
@ReisauthorMar 17.2010 — I understand, but its not a problem, this is for an application only used by me and my 2 work-mates ?
×

Success!

Help @Reis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...