Sum (SUM) of an irregularly formatted column

Asked

Viewed 683 times

1

In my Mysql table I have a column called "weight". This column has the following values:

19.325
14.369
15.325
15.369
17.698
19.258
18.098

I simply need to accomplish the sum, but when I perform select sum(peso) from tabela it returns me the value 119.44200000000001 instead of 119.442.

  • 3

    Try to use the function ROUND to round up the values, for example: SELECT ROUND(SUM(peso), 3) AS peso FROM tabela;

  • Valdeir, you were fast is right. Perfect, solved my problem. Thank you very much.

  • If you need exact numbers, the solution is to use exact decimal types, and no floating points. Round is a solution for sporadic and/or point situations.

2 answers

1

Dude, that depends a lot ,did you use float or double? But you can in create use decimal, example Create table name ( // here I am passing two houses after the comma

decimal weight(3, 2) not null, );

0

Try to do it this way...

SELECT FORMAT (SUM (weight), 'N', 'en') 'Weight' FROM table

Browser other questions tagged

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