-
I would like to insert this type of number format in to my database.
$value = "20.000,00";I tried with
FLOATandDOUBLE, but they can’t handle this. Only option left that I know which works isVARCHAR? -
Although if I do this when I am working with the numbers later and tries to subtract number from each other:
$value2 = "15.933,50"; $calc = $value - $value2;$calcis now4.067, it should be4.066,50– how can this be correct?
I would like to insert this type of number format in to my database.
Share
For financial numbers, you should use the DECIMAL type. It has a fixed precision and isn’t subject to the rounding problems of floating point numbers.
Never store a money number in a VARCHAR or a FLOAT.
If you want to introduce in the database your
$value, you must parse it so that you can introduce the number :To display a number you got from your DB as “20.000,00”, you may use
EDIT :
If you can’t use a NumberFormatter, you may use this to build a string that you can introduce in a DECIMAL field (in a float one too but don’t) :
This changes
"20.000,00"to"20000.00"which the database can understand.But it’s always dangerous to ask a database to parse the strings as numbers (you may change the locale later). I’d recommend you to parse the number in PHP and to explicitly specify the locale.