in Education by
I'm trying to achieve the same amount of precision (number of digits after decimal point) for results coming from a SELECT that does some equations. Is there any difference in terms of performance, good practice of use in case of achieving wanted precision between CAST() or ROUND() besides rounding by the last one? Is there any better alternative? For the sake of simplicity values in all examples below are hardcoded, they may or may not come from table's columns I'm using MySQL 8. If you run example E1 : -- E1 SELECT (41/99); -- 0.4141 you'll get 4 digits after decimal point. Is there any MySQL setting that can bring higher precision right out of the box so I don't need to use: -- E2 SELECT ROUND((41/99), 20); -- 0.41414141400000000000 -- or SELECT CAST((41/99) AS DECIMAL(21,20)); -- 0.41414141400000000000 How to get more decimal points precision from E2 in case data used for calculation is int? If you supply data with decimal point you get much higher precision: -- E3 SELECT ROUND((41.0/99.0), 20); -- 0.41414141414141414100 SELECT CAST((41.0/99.0) AS DECIMAL(21,20)); -- 0.41414141414141414100 It is important to me to avoid floating point datatypes due to their approximation of decimals. If the data be coming from table column the column will be decimal datatype. But data for calculations may be also hardcoded. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
MySQL uses 64-bit IEEE-754 floating point (aka DOUBLE) for its internal computations unless you specifically get it to use integer or decimal arithmetic by casting your constants. (So does Javascript.) When displaying numbers it does its best to render them in decimal as accurately as possible. With DOUBLE, that requires a conversion to decimal. If you don't want the default rendering accuracy, but rather want to control it yourself, you may use the ROUND() or TRUNCATE() functions. That's how you control it. There's not much performance penalty for using these functions. Many programmers who use plain SQL (rather than SQL retrieved by an application program in Java, PHP, or another language) always use one of the functions to retain control over the rendering. Test carefully if you're depending on DECIMAL-data-type accuracy in computations: MySQL really wants to do them in DOUBLE. Or, better yet, use a strongly typed language to gain precise control over your arithmetic.

Related questions

0 votes
    How will you find whether a number is represented in Decimal/Binary/Octal or Hexadecimal? * O Through base O ... O Through numbers Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
0 votes
    Write· a program to read one of the hex digits A-I’, and...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    Is "Select column from updated" available in "After Update" trigger? If not what other approach can be ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Is "Select column from updated" available in "After Update" trigger? If not what other approach can be ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    For select operation the ________ appear in the subscript and the ___________ argument appears in the paranthesis ... Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    My initial dataframe looks: library(tidyverse) df...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    My initial dataframe looks: library(tidyverse) df...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    In an order to perform a calculation in a spredsheet you need to use a Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    roblem #1: [35 pts] Profit calculation for an Avocado farm Write a program that helps a farmer predict his ... profit = 36226595 $ Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    a. b. Any formula in Excel starts saluctangulshows the formula used in the selected cell. A formula which is ... forma C. onerator Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    write a sort note about the calculation of the date in worksheet? Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    A bag contains identical balls 4 are blue 6 are black what is the probability that the black ball will be selected Select the correct answer from above options...
asked Nov 22, 2021 in Education by JackTerrance
...