Binary Number System Binary Number System A Binary Number is made up of only 0s and 1s. 110100 Example of a Binary Number There is no 2, 3, 4, 5, 6, 7, 8 or 9 in Binary! A "bit" is a single binary digit. The number above has 6 bits. Binary numbers have many uses in mathematics and beyond. In fact the digital world uses binary digits . How do we Count using Binary? Binary But then there is no symbol for 2 ... what do we do? Well how do we count in Decimal? This is the last digit in Decimal So we start back at 0 again, but add 1 on the left The same thing is done in binary ... Now start back at 0 again, but add 1 on the left ••• When we run out of digits, we ... ... start back at 0 again, but add 1 on the left And that is what we do in binary ... Start back at 0 again, but add 1 on the left ••• start back at 0 again, and add one to the number on the left... ...

but that number is already at 1 so it also goes back to 0 ... ... and 1 is added to the next position on the left ••••• Start back at 0 again (for all 3 digits), add 1 on the left ••••••••• See how it is done in this little demonstration (press play button): Decimal vs Binary Here are some equivalent values: Decimal: Here are some larger equivalent values: Decimal: "Binary is as easy as 1, 10, 11." Now see how you can use Binary to count past 1,000 on your fingers: In the Decimal System there are Ones, Tens, Hundreds, etc In Binary there are Ones, Twos, Fours, etc, like this: This is 1×8 + 1×4 + 0×2 + 1 + 1×(1/2) + 0×(1/4) + 1×(1/8) = 13.625 in Decimal Numbers can be placed to the left or right of the point, to show values greater than one and less than one.

10.1 The number to the left of the point is a whole number (such as 10) As we move further left, every number place gets 2 times bigger. The first digit on the right means halves (1/2). As we move further right, every number place gets 2 times smaller (half as big). Example: 10.1 The "10" means 2 in decimal, The ".1" means half, You can do conversions at Binary to Decimal to Hexadecimal Converter . Words The word binary comes from "Bi-" meaning two. We see "bi-" in words such as "bicycle" (two wheels) or "binocular" (two eyes). When you say a binary number, pronounce each digit (example, the binary number "101" is spoken as "one zero one", or sometimes "one-oh-one"). This way people don't get confused with the decimal number. A single binary digit (like "0" or "1") is called a "bit". For example 11010 is five bits long.

The word bit is made up from the words "binary digit" How to Show that a Number is Binary To show that a number is a binary number, follow it with a little 2 like this: 1012 This way people won't think it is the decimal number "101" (one hundred and one). Examples Example: What is 11112 in Decimal? The "1" on the left is in the "2×2×2" position, so that means 1×2×2×2 (=8) The next "1" is in the "2×2" position, so that means 1×2×2 (=4) The next "1" is in the "2" position, so that means 1×2 (=2) The last "1" is in the ones position, so that means 1 Answer: 1111 = 8+4+2+1 = 15 in Decimal Example: What is 10012 in Decimal?

The "1" on the left is in the "2×2×2" position, so that means 1×2×2×2 (=8) The "0" is in the "2×2" position, so that means 0×2×2 (=0) The next "0" is in the "2" position, so that means 0×2 (=0) The last "1" is in the ones position, so that means 1 Answer: 1001 = 8+0+0+1 = 9 in Decimal Example: What is 1.12 in Decimal? The "1" on the left side is in the ones position, so that means 1. The 1 on the right side is in the "halves" position, so that means 1×(1/2) So, 1.1 is "1 and 1 half" = 1.5 in Decimal Example: What is 10.112 in Decimal?

The "1" is in the "2" position, so that means 1×2 (=2) The "0" is in the ones position, so that means 0 The "1" on the right of the point is in the "halves" position, so that means 1×(1/2) The last "1" on the right side is in the "quarters" position, so that means 1×(1/4) So, 10.11 is 2+0+1/2+1/4 = 2.75 in Decimal "There are 10 kinds of people in the world, those who understand binary numbers, and those who don't." Representation of numbers Representation of Numbers IEEE double precision floating point Introduction When working with any kind of digital electronics in which numbers are being represented, it is important to understand the different ways numbers are represented in these systems. Almost without fail, numbers are represented by two voltage levels which can represent a one or a zero (an interesting exception to this rule is the new memory device recently announced by Intel which uses one of four possible voltage levels, thereby increasing the amount of information that can be stored in a given space).

The number system based on ones and zeroes is called the binary system (because there are only two possible digits). Before discussing the binary system, a review of the decimal (ten possible digits) system is in order, because many of the concepts of the binary system will be easier to understand when introduced alongside their decimal counterpart. You should all have some familiarity with the decimal system. For instance, to represent the positive integer one hundred and twenty-five as a decimal number, we can write (with the postivie sign implied). The subscript 10 denotes the number as a base 10 (decimal) number. 12510 = 1*100 + 2*10 + 5*1 = 1*102 + 2*101 + 5*100 The rightmost digit is multiplied by 100, the next digit to the left is multiplied by 101, and so on. Each digit to the left has a multiplier that is 10 times the previous digit. Hopefully this is all a review.

Some observations: To multiply a number by 10 you can simply shift it to the left by one digit, and fill in the rightmost digit with a 0 (moving the decimal place one to the right). To divide a number by 10, simply shift the number to the right by one digit (moving the decimal place one to the left). To see how many digits a number needs, you can simply take the logarithm (base 10) of the absolute value of the number, and add 1 to it. The integer part of the result is the number of digits. For instance, log 10(33) + 1 = 2.5. The integer part of that is 2, so 2 digits are needed. With n digits, 10n unique numbers (from 0 to 10n-1) can be represented. If n=3, 1000 (=103) numbers can be represented 0-999. Negative numbers are handled easily by simply putting a minus sign (-) in front of the number. This does lead, however, to the somewhat awkward situation where 0=-0.

We will avoid this situation with binary representations, but with a little bit of effort. Representing fractions is a simple extension of this idea. To wit, 25.43 10 = 2*10 + 5*1 + 4*0.1 + 3*0.01 = 2*101 + 5*100 + 4*10-1+ 3*10-2 The only pertinent observations here are: If there are m digits to the right of the decimal point, the smallest number that can be represented is 10-m. For instance if m=4, the smallest number that can be represented is 0.0001=10-4. After reading this dcoument you might want to learn something about binary arithmetic . Binary Representation of positive integers Binary representations of positive can be understood in the same way as their decimal counterparts. For example 8610 = 1*64 + 0*32 + 1*16 + 0*8 + 1*4 + 1*2 + 0*1 or Hexadecimal, Octal, Bits, Bytes and Words.

It is often convenient to handle groups of bits, rather than individually. The most common grouping is 8 bits, which forms a byte. A single byte can represent 256 (28) numbers. Memory capacity is usually referred to in bytes. Two bytes is usually called a word, or short word (though word-length depends on the application). A two-byte word is also the size that is usually used to represent integers in programming languages. A long word is usually twice as long as a word. A less common unit is the nibble which is 4 bits, or half of a byte. It is cumbersome for humans to deal with writing, reading and remembering individual bits, because it takes many of them to represent even fairly small numbers. A number of different ways have been developed to make the handling of binary data easier for us. The most common is hexadecimal. In hexadecimal notation, 4 bits (a nibble) are represented by a single digit. There is obviously a problem with this since 4 bits gives 16 possible combinations, and there are only 10 unique decimal digits, 0 to 9. This is solved by using the first 6 letters (A..F) of the alphabet as numbers.

The table shows the relationship between decimal, hexadecimal and binary. Decimal Signed Binary Integers It was noted previously that we will not be using a minus sign (-) to represent negative numbers. We would like to represent our binary numbers with only two symbols, 0 and 1. There are a few ways to represent negative binary numbers. The simplest of these methods is called ones complement, where the sign of a binary number is changed by simply toggling each bit (0's become 1's and vice-versa). This has some difficulties, among them the fact that zero can be represented in two different ways (for an eight bit number these would be 0000 0000 and 1111 1111)., we will use a method called two's complement notation which avoids the pitfalls of one's complement, but which is a bit more complicated. To represent an n bit signed binary number the leftmost bit, has a special significance. The difference between a signed and an unsigned number is given in the table below for an 8 bit number.

The value of bits in signed and unsigned binary numbers 1011 1111 1011 Let's carefully consider the last case which uses the number -5. As a 4 bit number this is represented as 1011 = -8 + 2 + 1 = -5 The 8 bit number is 1111 1011 = -128 + 64 + 32 + 16 + 8 + 2 + 1 = -5. It is clear that in the second case the sum of the contributions from the leftmost 5 bits (-128 + 64 + 32 + 16 + 8 = -8) is the same as the contribution from the leftmost bit in the 4 bit representation (-8) This process is refered to as sign-extension, and can be applied whenever a number is to be represented by a larger number of bits. In the 320C50 Digital Signal Processor, this typically occurs when moving a number from a 16 bit register to a 32 bit register.

Whether or not sign-extension is applied during such a move is determined by the sign-extension mode bit . Note that to store a 32 bit number in 16 bits you can simply truncate the upper 16 bits (as long as they are all the same as the left-most bit in the resulting 16 bit number - i.e., the sign doesn't change). Most processors even have two separate instructions for shifting numbers to the right (which, you will recall, is equivalent to dividing the number in half). The first instruction is something like LSR (Logical Shift Right) which simply shifts the bits to the right and usually fills a zero in as the lefmost bit. The second instruction is something like ASR (Arithmetic Shift Right), which shifts all of the bits to the right, while keeping the leftmost bit unchanged. With ASR 1010 (-6) becomes 1101 (-3). Of course, there is only one instruction for a left shift (since LSL is equivalent to ASL). Positive binary fractions The representation of unsigned binary fractions proceeds in exactly the same way as decimal fractions.

For example 0.62510 = 1*0.5 + 0*0.25 + 1*0.125 = 1* 2-1 + 0* 2-2 + 1* 2-3 = 0.1012 Each place to the right of the decimal point represents a negative power of 2, just as for decimals they represent a negative power of 10. Likewise, if there are m bits to the right of a decimal, the precision of the number is 2-m (versus 10-m for decimal). Though it is possible to represent numbers greater than one by having digits to the left of the decimal place we will restrict ourselves to numbers less than one. These are commonly used by Digital Signal Processors. The largest number that can be represented by such a representation is 1-2-m , the smallest number is 2-m. For a fraction with 15 bits of resolution this gives a range of approximately 0.99997 to 3.05E-5.

Note that this representationis easily extended to represent all positive numbers by having the digits to the left of the decimal point represent the integer part, and the digits to the right representing the fractional part. Thus 6.62510 = 110.1012 Convert 0.100 1001 from binary to decimal Convert 0.111 1111 from binary to decimal Convert 0.75 from decimal to a binary fraction Convert 0.65625 from decimal to a binary fraction Approximate 0.9 as a binary fraction (use 8 bits) Signed binary fractions Signed binary fractions are formed much like signed integers. We will work with a single digit to the left of the decimal point, and this will represent the number -1 (= -(20)). The rest of the representation of the fraction remains unchanged. Therefore this leftmost bit represents a sign bit just as with two's complement integers. If this bit is set, the number is negative, otherwise the number is positive. The largest positive number that can be represented is still 1-2-m but the largest negative number is -1. The resolution is still 1-2-m.

There is a terminology for naming the resolution of signed fractions. If there are m bits to the right of the decimal point, the number is said to be in Qm format. For a 16 bit number (15 bits to the right of the decimal point) this results in Q15 notation. A Quicker Method for Converting Binary Fractions. Another way to convert Qm numbers to decimal is to represent the binary number as a signed integer, and to divide by 2m. To convert a decimal number to Qm, multiply the number by 2m and take the rightmost m digits. Note, this simply truncates the number; it is more elegant, and accurate, but slightly more complicated, to round the number. Examples (all Q7 numbers): Convert 0.100 1001 to decimal. Take the binary number 0100 1001 (=7310), and divide by 27=128. The answer is 73/128=0.5703125, which agrees with the result of the previous exercise (Positive Binary Fractions).

Convert 1.100 1001 to decimal. Take the two's complements binary number 1100 1001 (=-5510), and divide by 128. The answer is -0.4296875, which agrees with the result of the previous exercise (Signed Binary Fractions). Convert 0.9 to Q7 format Multiply 0.9 by 128 to get 115.2. This is represented in binary as 111 0011, so the Q7 representation is 0.111 0011. This agrees with the result of the previous exercise (Positive Binary Fractions). Convert -0.9 to Q7 format Multiply -0.9 by 128 to get -115.2. The Q7 representation is 1.000 1101. This agrees with the result of the previous exercise (Signed Binary Fractions).

This section is not complete IEEE single precision floating point This section is not complete IEEE double precision floating point This section is not complete ← Representation of real numbers and integers Representation of real numbers and integers January 2017 Representing a number in a computer Representing (or encoding) a number means to express it in binary form. Representing numbers in a computer is necessary in order for it to be able to store and manipulate them. However, the problem is that a mathematical number can be infinite (as great as desired), but the representation of a number in a computer must occupy a predefined number of bits. The key, then, is being able to predefine a number of bits, and how to intepret them, so that they can represent the figure as efficiently as possible. For this reason, it would be foolish to use 16 bits to encode a character (65536 possibilities) when less than 256 bits are typically used. Representation of a natural number A natural number is a positive integer or zero. The choice of how many bits to use depends on the range of numbers to be used.

To encode the natural numbers between 0 and 255, all that is needed is 8 bits (a byte) as 28=256. Generally speaking, n-bit encoding can be used for representing natural numbers between 0 and 2n-1. To represent a natural number, having defined how many bits will be used to code it, arrange the bits into a binary cell, with each bit placed according to its binary weight from right to left, then "fill" the unused bits with zeroes. Representation of an integer An integer is a whole number which may be negative. The number must therefore be encoded in such a way as to tell if it is positive or negative, and to follow the rules of addition. The trick involves using an encoding method called twos complement. A positive integer or zero will be represented in binary (base 2) as a natural number, except that the highest-weighted bit (the bit on the far left) represents the plus or minus sign. So for a positive integer or zero, this bit must be set to 0 (which corresponds to a plus sign, as 1 is a minus sign).

Thus, if a natural number is encoded using 4 bits, the largest number possible will be 0111 (or 7 in decimal). Generally, the largest positive integer encoded using n bits will be 2n-1-1. A negative integer is encoded using twos complement. The principle of twos complement: Choose a negative number. Take its absolute value (its positive equivalent) It is represented in base 2 using n-1 bits Each bit is switched with its complement (i.e. the zeroes are all replaced by ones and vice versa) Add 1 Note that by adding a number and its twos complement the result is 0 Let's see this demonstrated in an example: We want to encode the value -5 using 8 bits. To do so: write 5 in binary: 00000101 switch it to its complement: 11111010 add 1: 11111011 Decimal Structure (System) Decimal Structure Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained.

Decimal Structure Remarks The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations that require large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding. For example, the following code produces a result of 0.9999999999999999999999999999 instead of 1.

Copy decimal dividend = Decimal.One; decimal divisor = 3; // The following displays 0.9999999999999999999999999999 to the console Console.WriteLine(dividend/divisor * divisor); When the result of the division and multiplication is passed to the Round method, the result suffers no loss of precision, as the following code shows. Copy decimal dividend = Decimal.One; decimal divisor = 3; // The following displays 1.00 to the console Console.WriteLine(Math.Round(dividend/divisor * divisor, 2)); A decimal number is a floating-point value that consists of a sign, a numeric value where each digit in the value ranges from 0 to 9, and a scaling factor that indicates the position of a floating decimal point that separates the integral and fractional parts of the numeric value. The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28.

Therefore, the binary representation of a Decimal value the form, ((-296 to 296) / 10(0 to 28)), where -(296-1) is equal to MinValue , and 296-1 is equal to MaxValue . For more information about the binary representation of Decimal values and an example, see the Decimal(Int32[]) constructor and the GetBits method. The scaling factor also preserves any trailing zeros in a Decimal number. Trailing zeros do not affect the value of a Decimal number in arithmetic or comparison operations. However, trailing zeros might be revealed by the ToString method if an appropriate format string is applied. Conversion Considerations This type provides methods that convert Decimal values to and from SByte , Int16 , Int32 , Int64 , Byte , UInt16 , UInt32 , and UInt64 values. Conversions from these integral types to Decimal are widening conversions that never lose information or throw exceptions. Conversions from Decimal to any of the integral types are narrowing conversions that round the Decimal value to the nearest integer value toward zero. Some languages, such as C#, also support the conversion of Decimal values to Char values.

If the result of these conversions cannot be represented in the destination type, an OverflowException exception is thrown. The Decimal type also provides methods that convert Decimal values to and from Single and Double values. Conversions from Decimal to Single or Double are narrowing conversions that might lose precision but not information about the magnitude of the converted value. The conversion does not throw an exception. Conversions from Single or Double to Decimal throw an OverflowException exception if the result of the conversion cannot be represented as a Decimal. Performing Operations on Decimal Values The Decimal type supports standard mathematical operations such as addition, subtraction, division, multiplication, and unary negation. You can also work directly with the binary representation of a Decimal value by calling the GetBits method. To compare two Decimal values, you can use the standard numeric comparison operators, or you can call the CompareTo or Equals method. You can also call the members of the Math class to perform a wide range of numeric operations, including getting the absolute value of a number, determining the maximum or minimum value of two Decimal values, getting the sign of a number, and rounding a number. Number Notation Number Notation one million one thousand thousands Some people use a comma to mark every 3 digits.

It just keeps track of the digits and makes the numbers easier to read. Beyond a million, the names of the numbers differ depending where you live, and also the context. The places are grouped by thousands in countries using the "short scale" (such as the United States, Australia and English-speaking Canada), and by the millions in countries using the "long scale" (such as France and Germany). For further details on usage by country, see this article . Name 1 with a googol of zeros Fractions Digits to the right of the decimal point represent the fractional part of the decimal number. Each place value has a value that is one tenth the value to the immediate left of it.

Number 1/100000 Examples: 0.234 = 234/1000 (said - point 2 3 4, or 234 thousandths, or two hundred thirty four thousandths) 4.83 = 4 83/100 (said - 4 point 8 3, or 4 and 83 hundredths) SI Prefixes (I with a bar is not used) V=5 Roman Numeral Calculator Examples: 1 = I 2 = II 3 = III 4 = IV 5 = V 6 = VI 7 = VII 8 = VIII 9 = IX 10 = X 11 = XI 12 = XII 13 = XIII 14 = XIV 15 = XV 16 = XVI 17 = XVII 18 = XVIII 19 = XIX 20 = XX 21 = XXI 25 = XXV 30 = XXX 40 = XL 49 = XLIX 50 = L 51 = LI 60 = LX 70 = LXX 

80 = LXXX 90 = XC 99 = XCIX There is no zero in the roman numeral system.

The numbers are built starting from the largest number on the left, and adding smaller numbers to the right. All the numerals are then added together. The exception is the subtracted numerals, if a numeral is before a larger numeral, you subtract the first numeral from the second. That is, IX is 10 - 1= 9. This only works for one small numeral before one larger numeral - for example, IIX is not 8, it is not a recognized roman numeral. There is no place value in this system - the number III is 3, not 111. Number Base Systems Decimals Decimals A Decimal Number (based on the number 10) contains a Decimal Point. First, let's have an example: Here is the number "forty-five and six-tenths" written as a decimal number: The decimal point goes between Ones and Tenths. 45.6 has 4 Tens, 5 Ones and 6 Tenths, like this: Now, let's discover how it all works ... Place Value It is all about Place Value ! When we write numbers, the position (or "place") of each digit is important.

the "7" is in the Ones position, meaning 7 ones (which is 7), the "2" is in the Tens position meaning 2 tens (which is twenty), and the "3" is in the Hundreds position, meaning 3 hundreds. "Three Hundred Twenty Seven" As we move left, each position is 10 times bigger! Tens are 10 times bigger than Ones Hundreds are 10 times bigger than Tens ... and ... As we move right, each position is 10 times smaller. From Hundreds, to Tens, to Ones But what if we continue past Ones? What is 10 times smaller than Ones? 110ths (Tenths) are! But we must first put a decimal point, so we know exactly where the Ones position is: "three hundred twenty seven and four tenths" but we usually just say "three hundred twenty seven point four" And that is a Decimal Number!

We can continue with smaller and smaller values, from tenths, to hundredths, and so on, like in this example: Have a play with decimal numbers yourself: Large and Small So, our Decimal System lets us write numbers as large or as small as we want, using the decimal point. Digits can be placed to the left or right of a decimal point, to show values greater than one or less than one. The decimal point is the most important part of a Decimal Number. Without it we are lost, and don't know what each position means. 17 whole number (such as 17) As we move further left, every place gets 10 times bigger. The first digit on the right means tenths (1/10). As we move further right, every place gets 10 times smaller (one tenth as big). See decimals on the Zoomable Number Line Definition of Decimal The word "Decimal" really means "based on 10" (From Latin decima: a tenth part). We sometimes say "decimal" when we mean anything to do with our numbering system, but a "Decimal Number" usually means there is a Decimal Point. Ways to think about Decimal Numbers ... ...

as a Whole Number Plus Tenths, Hundredths, etc We can think of a decimal number as a whole number plus tenths, hundredths, etc: Example 1: What is 2.3 ? On the left side is "2", that is the whole number part. The 3 is in the "tenths" position, meaning "3 tenths", or 3/10 So, 2.3 is "2 and 3 tenths" Example 2: What is 13.76 ? On the left side is "13", that is the whole number part. There are two digits on the right side, the 7 is in the "tenths" position, and the 6 is the "hundredths" position So, 13.76 is "13 and 7 tenths and 6 hundredths" ... as a Decimal Fraction Or we can think of a decimal number as a Decimal Fraction.

A Decimal Fraction is a fraction where the denominator (the bottom number) is a number such as 10, 100, 1000, etc (in other words a power of ten ) So "2.3" looks like: BigDecimal (Java Platform SE 7 ) public class BigDecimal extends Number implements Comparable < BigDecimal > Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale). The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal. The BigDecimal class gives its user complete control over rounding behavior. If no rounding mode is specified and the exact result cannot be represented, an exception is thrown; otherwise, calculations can be carried out to a chosen precision and rounding mode by supplying an appropriate MathContext object to the operation.

In either case, eight rounding modes are provided for the control of rounding. Using the integer fields in this class (such as ROUND_HALF_UP ) to represent rounding mode is largely obsolete; the enumeration values of the RoundingMode enum, (such as RoundingMode.HALF_UP ) should be used instead. When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED ), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. (This is the only behavior that was supported in releases prior to 5.) As a corollary of computing the exact result, the rounding mode setting of a MathContext object with a precision setting of 0 is not used and thus irrelevant. In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

When the precision setting is not 0, the rules of BigDecimal arithmetic are broadly compatible with selected modes of operation of the arithmetic defined in ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000 (section 7.4). Unlike those standards, BigDecimal includes many rounding modes, which were mandatory for division in BigDecimal releases prior to 5. Any conflicts between these ANSI standards and the BigDecimal specification are resolved in favor of BigDecimal. Since the same numerical value can have different representations (with different scales), the rules of arithmetic and rounding must specify both the numerical result and the scale used in the result's representation. In general the rounding modes and precision setting determine how operations return results with a limited number of digits when the exact result has more digits (perhaps infinitely many in the case of division) than the number of digits returned. First, the total number of digits to return is specified by the MathContext's precision setting; this determines the result's precision. The digit count starts from the leftmost nonzero digit of the exact result. The rounding mode determines how any discarded trailing digits affect the returned result.

For all arithmetic operators , the operation is carried out as though an exact intermediate result were first calculated and then rounded to the number of digits specified by the precision setting (if necessary), using the selected rounding mode. If the exact result is not returned, some digit positions of the exact result are discarded. When rounding increases the magnitude of the returned result, it is possible for a new digit position to be created by a carry propagating to a leading "9" digit. For example, rounding the value 999.9 to three digits rounding up would be numerically equal to one thousand, represented as 100×101. In such cases, the new "1" is the leading digit position of the returned result. Besides a logical exact result, each arithmetic operation has a preferred scale for representing a result. The preferred scale for each operation is listed in the table below. Preferred Scales for Results of Arithmetic Operations Operation public static final BigDecimal TEN The value 10, with a scale of 0. Since: ROUND_UP public static final int ROUND_UP Rounding mode to round away from zero. Always increments the digit prior to a nonzero discarded fraction. Note that this rounding mode never decreases the magnitude of the calculated value.

See Also: ROUND_DOWN public static final int ROUND_DOWN Rounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates). Note that this rounding mode never increases the magnitude of the calculated value. See Also: ROUND_CEILING public static final int ROUND_CEILING Rounding mode to round towards positive infinity. If the BigDecimal is positive, behaves as for ROUND_UP; if negative, behaves as for ROUND_DOWN. Note that this rounding mode never decreases the calculated value. See Also: ROUND_FLOOR public static final int ROUND_FLOOR Rounding mode to round towards negative infinity. If the BigDecimal is positive, behave as for ROUND_DOWN; if negative, behave as for ROUND_UP. Note that this rounding mode never increases the calculated value. See Also: ROUND_HALF_UP public static final int ROUND_HALF_UP Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for ROUND_UP if the discarded fraction is ≥ 0.5; otherwise, behaves as for ROUND_DOWN. Note that this is the rounding mode that most of us were taught in grade school.

See Also: ROUND_HALF_DOWN public static final int ROUND_HALF_DOWN Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. Behaves as for ROUND_UP if the discarded fraction is > 0.5; otherwise, behaves as for ROUND_DOWN. See Also: ROUND_HALF_EVEN public static final int ROUND_HALF_EVEN Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for ROUND_HALF_DOWN if it's even. Note that this is the rounding mode that minimizes cumulative error when applied repeatedly over a sequence of calculations. See Also: ROUND_UNNECESSARY public static final int ROUND_UNNECESSARY Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. If this rounding mode is specified on an operation that yields an inexact result, an ArithmeticException is thrown. See Also: BigDecimal public BigDecimal( String val) Translates the string representation of a BigDecimal into a BigDecimal.

The string representation consists of an optional sign, '+' ( '\u002B') or '-' ('\u002D'), followed by a sequence of zero or more decimal digits ("the integer"), optionally followed by a fraction, optionally followed by an exponent. The fraction consists of a decimal point followed by zero or more decimal digits. The string must contain at least one digit in either the integer or the fraction. The number formed by the sign, the integer and the fraction is referred to as the significand. The exponent consists of the character 'e' ('\u0065') or 'E' ('\u0045') followed by one or more decimal digits. The value of the exponent must lie between - Integer.MAX_VALUE ( Integer.MIN_VALUE +1) and Integer.MAX_VALUE , inclusive. More formally, the strings this constructor accepts are described by the following grammar: BigDecimalString:

Question: What decimal number is represented by the binary 101 (read as one zero one)?
Answer: Five