How I supposted to store number in db to get two decimal places without rounding Hi,
I would like to simply store my numbers in my input format (34.12, 12.32, etc.)
The problem is that every data type in mysql is rounding my numbers.
I have already tried decimal(4,2), decimal(2,2), float(2,2), float(9,2).
What datatype I supposted to use to get my real value?
Store an integer instead. I see you always have two numbers behind the comma so you can then do number / 100 and you have the correct value again.
If you really want it to work with a random amount of numbers in front or behind the comma you can store two values in the database. One for the number before the comma and one for behind the comma.
Store an integer that is the result of your input times 100.
When you have to present it, divide by 100.
It is a common practice to handle currency decimals where there should be no rounding.
@bobbybouwmann @otepas
$table->integer('my_number');
When I use integer I get only first 2 digits, so I get 38 from 38.75. These two decimal values are important for me.
Its working with string, I put 38.75 and I get in db 38.75 but this is probably not right thing to do.
You need to store 3875 in the database.
Create a mutator in your model that multiplies the input by 100 on set and an accessory that divides by 100 on get
@lipa
Decimal data type should not round your numbers!
I do use it on all my decimal fields.
decimal(DIGITS, PRECISION)
For example: decimal(4,2) will store up to 99.99. decimal(2,2) will store only 0.99.
If the data is being rounded, maybe you are using the wrong decimal separator!
Please sign in or create an account to participate in this conversation.