0

I would like to know the proper way to insert decimals to a MySQL database

When I use the type decimal and if I enter 0.20 to the database its saving the value as 0

And when I use the type float and if I enter 0.20 to the database its saving the value as 0.2

I want to display the same value that enter and also, I want to use this value for filters. So, I don’t want to use the varchar type to inset these values.

Can someone tell me what is the best way to do this? I wouldn't mind using PHP to display the missing end zero.

Appreciate your time.

4
  • 5
    With the decimal type, you specify the length and number of decimal places. How did you define the column in your example? Commented Jul 20, 2017 at 16:20
  • @SloanThrasher i'm using the default. which is (10,0) Commented Jul 20, 2017 at 16:22
  • 2
    10,0 means 10 characters 0 of which are decimal 10,2 would mean 8 numbers to the left, 2 to the right of the decimal. Float should only be used when you're dealing with extremely large or small numbers. Like the number of atoms in the galaxy where precision doesn't matter but scale does. Lastly don't mix the concepts of "Display"(format) and value. Display is a formatting matter when dealing with numbers. 0005.50000 is the same as 5.5. The value is the same it's just a matter of format/display. Commented Jul 20, 2017 at 16:24
  • Yes seem 10,2 is working. If you can add this as a answer i can accept it. Commented Jul 20, 2017 at 16:27

1 Answer 1

3

10,0 means 10 characters 0 of which are decimal. 10,2 would mean 8 numbers to the left, 2 to the right of the decimal.

Float should only be used when you are dealing with extremely large or small numbers; like the number of atoms in the galaxy where precision does not matter but scale does.

Lastly don't mix the concepts of "Display"(format) and value. Display is a formatting matter. The value 0005.50000 is the same as 5.5. Their display is just a matter of formating.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.