Mysql decimal value

Asked

Viewed 53,548 times

13

I am trying to insert decimal value in mysql database, wanted to put these values for example:

1.500,00

Thousand, hundred and ten

What is the type of column I must declare?

  • 1

    It is for monetary value or real numbers only ?

  • 2

    You are only by storing values numbers by specifying the tab Top which is the point .. To leave it in the desired format it is necessary to use a class of a programming language.

  • real numbers @gmsantos

5 answers

16

One of the options is to use DECIMAL( tamanho, casas ), but I particularly recommend using INT and multiply the values by 100.

But first of all, you have to convert the value to a number with points in place of the decimal separator, which can be done this way:

-- Usando DECIMAL(6,2) por exemplo:
INSERT INTO
   minhatabela
SET
   campodecimal = REPLACE( REPLACE( '1.500,00', '.' ,'' ), ',', '.' )

or for whole:

-- Usando INT(10) por exemplo:
INSERT INTO
   minhatabela
SET
   campointeiro100 = 100 * REPLACE( REPLACE( '1.500,00', '.' ,'' ), ',', '.' )

and in this second case, you can use this syntax to retrieve the data by reversing the calculation:

SELECT campointeiro100 / 100 FROM minhatabela

The explanation is as follows:

  • The internal replace swaps the points for nothing, leaving the value like this: 1500,00;
  • the external replace changes the comma by point, leaving the value like this: 1500.00, which is the format Mysql expects for decimal numbers.
  • Both INT and DECIMAL have no floating point storage problems, and are suitable for financial/monetary use.

The INT has the advantage of being much more simplified, and the DECIMAL has the advantage of appearing formatted on the screen in the desired way, but only using SQL query tools.

As the use is usually done through an application, it is much more interesting to leave the screen formatting in charge of the application, so the INT usually turns out to be more advantageous in terms of space and speed. Remember that in this case, the REPLACE and the * 100 can already be done directly on the application side, by a function of its own, leaving the use of INT transparent.

On the other hand, the DECIMAL less work if you need to change the number of houses in the future. Question apply the most convenient to the practical case.

  • The INT guarding 1500.00, what would be more suitable to work with formatting and internationalization transforming into 1,500.00, 1.500,00, 1 500,00, correct?

  • 1

    @Papacharlie for money, for example, if the value is 1.500,00 I create a name field valor100 and I keep 150000 cents. The name value100 is a facilitator that I adopted to know that it is the integer multiplied by 100. Likewise, I usually use a field quantidade1000, where "one kilo" of the product would be 1000, half a litre of fuel is 500, etc. (the unit in the field name is not required, but it is a great reminder. as the only int problem I know is the fact that I have to remember the splitter, I solved so)

  • 1

    As for formatting, there are many ways to do it, it depends more on the client language. But in almost any of them a function to display the way you want it is usually very simple and efficient, this when the language does not have a ready, which is rare.

  • Interesting strategy, in your case is INT right? About formatting, the application would be to internationalize the same value, since in some places the thousand is ., others ,, a space others - as I quoted above. It would be more practical to use a printf in having to exchange points for commas...

  • 1

    @Papacharlie has many ways, you can take value/100 and value%100 if you want to separate the pennies from the rest, or you can just divide by 100 and use the language point if it is already internationalizable. Anyway, I adopt the int by being compatible with anything and simple to use. In some cases, the decimal can be interesting if the language support is good (but rarely use).

  • I’m using decimal, but with reservations... I liked the use of INT, I’ll adopt her :) TKS Bacco

  • 1

    @Papacharlie at least will be an interesting experience to know the pros and cons :)

Show 2 more comments

10

Type DECIMAL(7,2) The fastest and easiest way.

The 2 indicates decimal places.

The 7 indicates maximum numerical quantity before comma.

  • Why use decimal instead of double?

  • @Danielaccorsi for example, I’m inserting 1,500,20 he’s inserting 1.50

  • @gmsantos, the problem of using double/float for monetary values is that after a value (a few million) it starts to lose the precision of decimals.

  • @Furlan does not know which type you used, but usually this occurs when you define a decimal type with lower numerical capacity than desired. Mysql will automatically reduce the number. Try increasing the capacity on.

  • If for the application it is not important to have this level of decimnal precision, I must use Decimal ?

  • 2

    @gmsantos float and double work with floating point, its accuracy is not as accurate as decimal. Remembering that double and float work through internal calculations to arrive at the expected result. Decimal, on the other hand, works "native" to the values of the users, consequently requiring less server consumption. Decimal is fixed point, not floating.

  • but what exact type for me to use?

  • @Furlan use DECIMAL(7,2) - you will have a large margin for your values. And you will not lose in long-term precision.

  • 2

    In his example, the 7 indicates the total number of digits. DECIMAL(7,2) has 2 decimal places, and 5 digits before them.

Show 4 more comments

10

In Mysql we have some types of data to store decimal numbers:

FLOAT and DOUBLE

Both represent numbers with floating points.

The difference between both is in their accuracy and in the size in bytes of each. The FLOAT has an accuracy of approximately 7 decimal places, whereas the DOUBLE up to 14, which results in double disk size compared to the FLOAT.

DECIMAL

Decimal numbers store a decimal number with exact precision.

In Mysql a decimal type can store a maximum of 65 digits, up to 30 digits after decimal place.

But after all, which of the guys to use?

It depends on your application. For applications working with monetary values the accuracy is very important,DECIMAL is safer.

For applications where accuracy is not important, or scientific calculations, go from FLOAT or DOUBLE.

Remember that Mysql works with numbers in the American standard. Soon you need to convert , for . as indicated in that reply.

7

Store the number as decimal to not lose the accuracy of decimals in monetary values. To insert/update make a formatted number conversion 1.500,00 first remove all the dots and finally replace the comma by dot at the end it will look like this: 1500.00. In a select you can use the code below

SELECT concat('R$ ', format(15000, 2)) as valor;

exit:

R$ 15,000.00

Example

Based on:

mysql - format()

Soen

0

Use the Mysql Decimal value with its value indicated as 10.2

decimal(10,2)   
  • Greetings Rodrigo, this has not been answered in the other answers?

  • 1

    Oops, say hello to Anderson. In fact it was answered, however, I pointed out a solution in dealing with monetary value that was useful to me, I just thought it would be useful for those who would read too.

  • 1

    I could complete the answer by justifying why the figures are 10 and 2?

  • 1

    10.2 on account of two decimal places after comma, and for the display of the same in a query we could use the following command FORMAT(IMO_VALOR_ALUGUEL, 2, 'de_DE') AS Rent which was what I used in a situation in my current job.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.