Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale and Precision from Number

I'm looking to get the scale and precision from a number in the following example.

var x = 1234.567;

I'm not seeing any .scale or .precision functions built in and I'm not sure what the best way to right one is.

like image 846
Code Junkie Avatar asked Jun 08 '12 16:06

Code Junkie


People also ask

What is precision and scale in number oracle?

Oracle guarantees the portability of numbers with precision of up to 20 base-100 digits, which is equivalent to 39 or 40 decimal digits depending on the position of the decimal point. s is the scale, or the number of digits from the decimal point to the least significant digit. The scale can range from -84 to 127.

What is precision and scale in float?

Decimal floating-point has distinct values for a number and the same number with various exponents (for example: 0.0, 0.00, 0.0E5, 1.0, 1.00, 1.0000). The precision is the total number of decimal digits, excluding the sign. The scale is the total number of decimal digits to the right of the decimal point.

Can Scale be greater than precision?

Scale specifies the maximum number of digits that can be stored to the right of the decimal point. Scale must be less than or equal to the precision. You can specify a scale ranging from 0 digits to 18 digits, or use the default scale of 0 digits.

What is the precision of an integer?

The precision of binary integers and decimal numbers is the total number of binary or decimal digits excluding the sign. The precision of floating-point numbers is either single or double, based on the number of hexadecimal digits in the fraction. A small integer is a binary integer with a precision of 15 bits.


1 Answers

You can use:

var x = 1234.56780123;

x.toFixed(2); // output: 1234.56
x.toFixed(3); // output: 1234.568
x.toFixed(4); // output: 1234.5680
like image 100
Jocelyn B. Avatar answered Sep 28 '22 22:09

Jocelyn B.