-3

Please help me. I have division problem on mysql. I have query like this :

SELECT (8/10) as res

This query result is 0.8000 and I want the result is 0.8 not 0.8000. Does someone have a solution?

Al-kadafi
  • 31
  • 1
  • 4
  • 2
    80/10 = 8.0000? – P.Salmon Aug 10 '18 at 07:51
  • 2
    Possible duplicate of [Format number to 2 decimal places](https://stackoverflow.com/questions/11190668/format-number-to-2-decimal-places) – juzraai Aug 10 '18 at 07:51
  • `SELECT (80/10) as res` this query results in `8.0000` and `SELECT (80/100) as res` results in `0.8000` and `SELECT ROUND((80/100), 1) as res` results in `0.8` – codtex Aug 10 '18 at 07:51
  • @codtex how with 15/100 ? it will result 0.2. i wanna remove 0 after 8, so the result is 0.8 or with case 15/100, the result is 0.15 not 0.1500 – Al-kadafi Aug 10 '18 at 08:14

2 Answers2

0

You are not very clear but I think the following queries does what you want:

SELECT TRIM(TRAILING '0' FROM  ( 80/100 )) as res;
# Output is 0.8

SELECT TRIM(TRAILING '0' FROM  ( 15/100 )) as res;
# Output is 0.15

It will remove the trailing 0 from the result.

The Drawback

If you do for example:

SELECT TRIM(TRAILING '0' FROM  ( 81/11 )) as res;
# Output is 7.3636

So the question is what exactly do you want to do? Do you need rounding at all?

codtex
  • 6,128
  • 2
  • 17
  • 34
  • thanks @codtex, i will try it. actually, i have a long query for get value of transaction, even if my query is split, this is only a calculation of ((80/100) * 1) - 0.2. and the result isn't 0.6 but 0.6000000000000001. i think division is the problem, so i ask like that. – Al-kadafi Aug 10 '18 at 08:31
-1

EDIT :Trim with add 0 value

SELECT TRIM((80/100))+0 
  • how with 15/100 ? it will result 0.2. i wanna remove 0 after 8, so the result is 0.8 or with case 15/100, the result is 0.15 not 0.1500 – Al-kadafi Aug 10 '18 at 08:18
  • you question are not clear.. You can use trim with add 0 like this SELECT TRIM((80/100))+0 – Yogi Prayoga Aug 10 '18 at 08:55
  • oke thanks, sorry. that's solve for division problem. but i wanna ask, how about subtraction, i get result 0.6000000000001 of ((80/100)*1)-0.2 operation, i get value 80,100,1, 0.2 from query too. what's the problem? – Al-kadafi Aug 10 '18 at 09:23
  • owh..may you try with this query; SELECT TRIM(((80/100)*1)-0.2)+0 ... it will give you result 0.6 – Yogi Prayoga Aug 10 '18 at 10:34