In Mysql which field type is suitable for storing weight in grams?

Asked

Viewed 1,452 times

0

I have this doubt, what kind of field to use for weight like 0.900, 1.200, 2.000, 0.100 and be able to treat the fields as number in the query output?

  • Use a float

  • How to focus the float pattern for unit weight ?

  • You will treat the values where?

  • If it’s php, it can be up to a varchar, when you receive the variable you can handle the way you want it

  • I need to store the values as quoted in the question, 0.900, 1.200, 2.400, 0.100, always 3 decimal places after the point, and up to 4 decimal places before the point.

  • Yes, I recommend varchar, I think all numeric types will cut if you have a 0 after the last number, I can’t tell you that for sure, but when dealing with php for example, da para fazer o que quiser

  • It is giving work with varchar, I defined as double 10,3 same. It is responding well.

  • 0.010, 1.000, 0.301, 0.001, is holding normal, thank you brother.

  • At vdd I didn’t even help kkkk, I don’t have much experience with dbs, but I hope it all works out there

  • It is that if you define as varchar, then it becomes more laborious to treat these values as integers, for example, to perform sum for a freight calculation, then you have to make maneuvers, if you keep in fields of the type that treats numbers, less code, thank you.

  • 5

    First of all, "weight in grams" is common in S.I. In question you speak in grams, but 0.900 seems to me to be saying in kilograms. Or is that right, less than one gram? So it would be zero comma nine ranges, that is, nine hundred milligrams? If it is to store in grams even, usually the best field is integer. If it is fractional, bullshit store in grams, choose smaller unit. What would not have fit would use float or string, whatever the case.

  • 1

    @Bacco has an interesting case to use float, in fact the only appropriate case to use this kind of data: scientific calculations that can lose accuracy within an acceptable limit (normally related to the limit of measurement; if the limit of weight measurement is 5 mg, then there is an intrinsic measurement error of 2.5 mg and any further accuracy is no longer relevant to the measurement; another case is when the measurement is obtained indirectly, then there is error that comes carried not only by the measure itself, but also by the mathematical operation, as standard deviation). But scientific here doesn’t seem to be

  • 2

    @Jeffersonqueso in fact I oversimplified in "what was the case", I could think some valid cases myself, but I fell into the rhetoric by the space of the comment + context. I mean, you’re right, this one you mentioned is a valid context.

Show 8 more comments

2 answers

6

The question doesn’t have enough information to nail a guy. It also states that you have to record a certain way, but it doesn’t say why. I see in a lot of situations the person thinks it has to be that way, but it could be otherwise. The question does not speak if 0.900 are 900 grams or 900 milligrams.

I would write as an integer, or write with the smallest possible unit, probably milligrams, so I don’t depend on the decimal part. And would treat in the application about this. But nothing prevents the use of a decimal type, and this type should probably be the DECIMAL. The FLOAT or DOUBLE may be appropriate if the problem allows inaccuracy, I I prefer to work with exact numbers.

In the same way that should not use a numeric type for descriptive data just because they only accept digits, should not use a text type for quantitative data.

  • Check the title: "In mysql which field type is suitable for storing weight in GRAMS?" And in the body of the question, I used grams, because the question is about grams. And I treat the entry in DB before storing, for any "inaccuracies", but it worked, thanks for everything, for your point of view that has added knowledge to my work.

  • 5

    I still do not know if . 900 are 900 grams or 900 milligrams, as well as 1,200 are 1.2 grams or if it is 1.2 Kg, the ambiguous continuous information. For me . 900 is 900 milligrams and so I would record the milligrams. To work is different from being right. Since I didn’t see what you did, I can’t say if it just worked or if it’s right. I work with information precision, only so you can know if it’s right or not. When the information is incomplete or ambiguous you can’t trust the result, you’re only making a bet that can work just by coincidence.

  • The discussion generated made me learn between the "needs to be like this" or what is semantic, I believe that the misunderstanding of the question with the body of doubt ended up understanding beyond what it needed for the case. grateful

5


I believe the fact that it’s in grams do not say much, because there is a type of field for each type of measure: gram, kilo, meter, kilometer etc.

You can use the type decimal, which is recommended for values involving decimals:

          -------------↓ 3 decimais
          ↓           ---
decimal(4,3) // ex. 0.900
        ↑           -----  
        --------------↑ 4 dígitos no total, sendo 1 inteiro

Browser other questions tagged

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