0

i'm using $_POSTto send form values to PHP and then insert into database, i have some inputs for prices values that looks like this:

1.000.000,00 (1 million in BRL "Brazilian Real")

I need to convert it to 1000000.00 (DECIMAL(10,2) to insert into database)

How can i do that using PHP number_format()?

Thanks!

EDIT

If number_format() is not the function i'm looking for, what's best to be using in this case?

Also, i need help finding a solution, even if the user value is 100.000,00

5 Answers 5

3
  1. You can not do that with number format, it works other way around.
  2. The thing that you do is bad practice, View layer should send primitive data to Controller - if you are using some advanced javascript component to represent to the user formatted number, that is fine, but underlaying form control should send primitive data, i.e. 10000000.00

Now, if nothing that I have stated to you is not applicable at this particular moment, and having in mind that this format that you have send is fixed (dot for thousand separator, coma for decimal separator), you can clean the value by using simple str_replace.

But, the trick is to replace first dot with empty string, then coma with dot.

str_replace(',', '.',str_replace('.', '', $number));

Got it?

But know that what you are doing is bad approach and wrong implementation, eventually, it will bite you in the a**.

Sign up to request clarification or add additional context in comments.

4 Comments

i'm using jQuery Mask plugin, so the user can't insert anything inside these inputs, and the value goes as the Mask, that's why i need to clean it up and convert to save into database.
@WilliamXavier I haven't used the jQuery Mask plugin, but it doesn't seem very likely that it would be so inflexible that you can't somehow get the unformatted value from the element. I agree with this answer, completely. It should not be something you do with PHP.
I usually build my own jQuery plugins for form controls. My usual practice is to have "visible" form field (e.g. text field WITHOUT name attribute - it will not get send via submit) and hidden for field. Now, when visible field is changed, hidden gets updated with clean value. On form submit - hidden field gets send to server with clean, primitive value. That is my approach, which I highly recommend.
For majority plugins -> you can just wrap them with your plugin, see example: github.com/RunOpenCode/dmDateTimePickerPlugin so you do not have to write your own to achieve the same.
2
<?php
$number  = '1.000.000,00';
$replaced_number = str_replace(array('.',','), array('',''), $number);
echo number_format($replaced_number,2,'.','');
?>

Comments

2

The easiest way is to use str_replace() function:

<?php
$p = '1.000.000,00';
$p = str_replace('.', '', $p);
$p = str_replace(',', '.', $p);
echo $p;

Comments

0

At first replace the (.) and (,) with str_replace by '' and then use t the following function

number_format($replaced_number,$decimal)

Comments

0

If you have a look at php.net you will easily see the right syntax for your goal: number_format($number,2,'.','') The first parameter is the number of decimal places that in your case is 2. The second is the symbol to use for decimal separator and the third is the one to be used for thousands that in your case will be nothing .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.