MySQL Forums
Forum List  »  Portuguese

Re: Update casas decimais
Posted by: Marcelo Altmann
Date: May 21, 2015 07:14AM

Olá Daniel,
Você pode usar a função floor que não lhe trará problemas com arredondamento - http://dev.mysql.com/doc/refman/5.6/en/mathematical-functions.html#function_floor

mysql> CREATE table teste (valor DOUBLE);
Query OK, 0 rows affected (0.06 sec)

mysql> INSERT INTO teste VALUES (10.8888), (11.2548), (100.2348);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM teste;
+----------+
| valor |
+----------+
| 10.8888 |
| 11.2548 |
| 100.2348 |
+----------+
3 rows in set (0.00 sec)

mysql> UPDATE teste SET valor = FLOOR(valor);
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0

mysql> SELECT * FROM teste;
+-------+
| valor |
+-------+
| 10 |
| 11 |
| 100 |
+-------+
3 rows in set (0.00 sec)


Ou você pode usar a função CAST, CONVERT que irá arredondar os valores - https://dev.mysql.com/doc/refman/5.6/en/cast-functions.html

mysql> TRUNCATE teste;
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO teste VALUES (10.8888), (11.2548), (100.2348);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM teste;
+----------+
| valor |
+----------+
| 10.8888 |
| 11.2548 |
| 100.2348 |
+----------+
3 rows in set (0.00 sec)
mysql> UPDATE teste SET valor = CAST(valor as SIGNED);
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0

mysql> SELECT * FROM teste;
+-------+
| valor |
+-------+
| 11 |
| 11 |
| 100 |
+-------+
3 rows in set (0.00 sec)

mysql> TRUNCATE teste;
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO teste VALUES (10.8888), (11.2548), (100.2348);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> UPDATE teste SET valor = CONVERT(valor, SIGNED);
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0

mysql> SELECT * FROM teste;
+-------+
| valor |
+-------+
| 11 |
| 11 |
| 100 |
+-------+
3 rows in set (0.00 sec)

Options: ReplyQuote


Subject
Views
Written By
Posted
2355
March 25, 2015 09:42AM
1164
March 31, 2015 07:36AM
Re: Update casas decimais
2625
May 21, 2015 07:14AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.