REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 7-Aug-2010 Edit History  

REBOL 3 Datatypes: Decimal!

Contents

Concept

The decimal! datatype is based on 64-bit standard IEEE 754 binary floating point numbers. They are distinguished from integer numbers by a decimal point (a period or a comma is allowed for international usage, see the notes below).

Format

Decimal values are a sequence of numeric digits, followed by a decimal point, which can be a period (.) or a comma (,), followed by more digits. A plus (+) or minus (-) immediately before the first digit indicates sign. Leading zeros before the decimal point are ignored. Extra spaces, commas, and periods are not allowed.

1.23
123.
123.0
0.321
0.123
1234.5678

A comma can be used in place of a period to represent the decimal point (which is the standard in many countries):

1,23
0,321
1234,5678

Use a single quote (`) to separate the digits in long decimals. Single quotes can appear anywhere after the first digit in the number, but not before the first digit.

100'234'562.3782
100'234'562,3782

Do not use commas or periods to separate the digits in a decimal value.

Scientific notation can be used to specify the exponent of a number by appending the number with the letter E or e followed by a sequence of digits. The exponent can be a positive or negative number.

1.23E10
1.2e007
123.45e-42
56,72E300
-0,34e-12
0.0001e-001

Decimal numbers span from 2.2250738585072e-308 up to 1.7976931348623e+308 and can contain up to 15 digits of precision.

Creation

Use the to-decimal function to convert string!, integer!, block!, or decimal! datatypes to a decimal number:

probe to-decimal "123.45"
123.45
probe to-decimal 123
123
probe to-decimal [-123 45]
-1.23E+47
probe to-decimal [123 -45]
1.23E-43
probe to-decimal -123.8
-123.8
probe to-decimal 12.3
12.3

If a decimal and integer are combined in an expression, the integer is converted to a decimal number:

probe 1.2 + 2
3.2
probe 2 + 1.2
3.2
probe 1.01 > 1
true
probe 1 > 1.01
false

Related

Use decimal? to determine whether a value is an decimal! datatype.

print decimal? 0.123
true

Use the form, print, and mold functions with a decimal argument to print a decimal value in its simplest form:

For example,

probe mold 123.4
123.4
probe form 2222222222222222
2.22222222222222E+15
print 1.00001E+5
100001

Single quotes (`) and a leading plus sign (+) do not appear in decimal output:

print +1'100'200.222'112
1100200.222112


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin