CDbl and decimal places

# Thread: CDbl and decimal places

1. Senior Member
Join Date
Dec 1969
Posts
220

## CDbl and decimal places

Is it possible to return a number with more decimal places than the CDbl(x) function? For example, if I have a variable like pi, and use CDbl(pi), it returns the following:<BR>3.14159265358979<BR><BR>What if I need more than just 14 decimal places? Not that I ever would, I&#039;m just curious.<BR>

2. Senior Member
Join Date
Dec 1969
Posts
2,092

## RE: CDbl and decimal places

Try FormatNumber(), FormatCurrency(), ...

3. Senior Member
Join Date
Dec 1969
Posts
220

## RE: CDbl and decimal places

formatnumber(pi,30) returned the following:<BR>3.141592653589790000000000000000<BR> <BR>Again, accurate only up to 14 decimal places.<BR><BR>Formatcurrency returns the same number, only with a dollar sign in front of it.

4. Senior Member
Join Date
Dec 1969
Posts
2,092

## RE: CDbl and decimal places

Yes... that&#039;s why I said *Try* as in maybe it could&#039;ve helped but i guess not!

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Understanding numbers...

A DOUBLE uses 8 bytes of memory.<BR><BR>For most computer languages--including JS and VBS--a double provides you with the greatest range of values. In many languages--again, including VBS and JS--double *also* provides the greatest amount of accuracy. <BR><BR>This is primarily because double is one of the data types implemented *IN THE HARDWARE* of most computer chips (and that has been true since the days of the first IBM 360&#039;s--about 1966 or so). And, again, it typically has the largest range and accuracy of all the hardware types. [That&#039;s slowly changing, but you can expect it to take another 5 years or so to trickle down to a desktop machine.]<BR><BR>The other data types that are done in hardware in a PC are SINGLE (4 byte floating point), LONG (4 byte integers), INT (2 byte integers), byte (not used in VBS or JS), and bit (ditto).<BR><BR>These are often referred to as the "primitive" types. Meaning there are "primitives" in the machine instruction set that can operate on them.<BR><BR>Here&#039;s a summary of the types as they are available from VBS:<BR>http://msdn.microsoft.com/library/en-us/script56/html/vbsDataType.asp<BR><BR>Back to double: In 8 bytes, 11 bits are used just to remember the "exponent" (that is, the power of two, which says where the binary point should be placed) and 53 remember the "mantissa".<BR><BR>In other words, the computer can represent any *INTEGER* of up to 53 bits accurately. Or (using 2^52, since we need a bit for the sign) *roughly* 4,503,599,625,000,000. That&#039;s 15 digits (plus 0 to 4 in the 16th digit, but since we can&#039;t count on the <BR>value in that first digit, we ignore it).<BR><BR>The "exponent" field is then used to multiply or divide that by by powers of two, but that doesn&#039;t change the basic limitation that you can&#039;t get more than 15 decimal digits of integer accuracy out of 53 bits.<BR><BR>And the routine in VBS (and JS) that converts an internal format number to human-readable form is aware of this limitation and so never tries to produce more than 15 digits, decimal, from that internal binary format.<BR><BR>So...Yep, you can only "see" 14 digits to the right of the decimal point when you have one digit to the left of it. If you had 7 digits to the left of the decimal point, you&#039;d only "see" 8 digits to the right. Etc. 15 digits total.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•