NumberFunctionDescriptions

advertisement
Number Functions Details
This is a guide that describes the functions found in the advancedMath helper object on the Duke Alice
in Schools Website!
truncateNumber
o The purpose of this function is to help display numbers in string form and remove excess
trailing decimal places.
1. For example: 3.789563201 and 2 decimal places will be returned as 3.78
o NOTE: This function DOES NOT round up when needed. See roundDecimal
HOW IT WORKS
1. This function takes in a number (decimal number) along with the number of
decimal places the user wants to be displayed.
2. A variable is used to store the integer form of the number entered. This saves
the number that will come before the decimal.

3.789563201 will be saved as 3
3. Another variable is then used to store the number entered multiplied by 10
raised to the number of decimals wanted

3.789563201 and 2 decimal places will be saved as 378.9563201
4. Then another variable is used to subtract the number that comes before the
decimal multiplied by 10 raised to the number of decimals wanted from the
number generated in Step 3. This is converted to an integer and gives the digits
that come after the decimal

378.9563201 – 300 = 78.9563201

Converted to integer gives 78
5. The variables that stored the integers before and after the decimal are
converted to strings and combined with a decimal point between them

3 and 78 become 3.78
6. The entire string is returned by the function!
roundDecimal
o The purpose of this function is to help display decimal numbers in string form by
removing excess trailing decimal places and rounding the last digit if needed.

For example: 4.5287 and 3 decimal places will be returned as 4.529
HOW IT WORKS
1. This function takes in a decimal number along with the number of decimal places
the user wants to be displayed.
2. A variable is used to store the integer form of the number entered. This saves
the number that will come before the decimal.

4.5287 will be saved as 4
3. Another variable is then used to store the number entered multiplied by 10
raised to the number of decimals wanted

4.5287 and 3 decimal places will be saved as 4528.7
4. Then a check is done to determine if rounding is needed. If the number
generated in Step 3 subtracted by the integer form of that same number is
greater than or equal to 0.5, one needs to be added to that number. (If decimal
trailing number generated in Step 3 is greater than or equal to 0.5).

4528.7 – 4528 = 0.7

So one must be added giving 4529.7
5. Another variable is used to subtract the (number that comes before the decimal
multiplied by 10 raised to the number of decimals wanted) from the number
generated in Step 3 or Step 4. This is converted to an integer and gives the digits
that come after the decimal
 4529.7 – 4000 = 529.7
 Converted to integer gives 529
6. The variables that stored the integers before and after the decimal are
converted to strings and combined with a decimal point between them
 3 and 78 become 3.78
7. The entire string is returned by the function!
Download