By EasyHow
How to round a number to the hundredths
The task of rounding to the hundredths sometimes appears in programmers. This happens in two cases. First, the language used may not be the corresponding rounding function. Secondly, an inexperienced programmer may not be aware of the subtleties of language. In either case, rescued a 4-step rounding algorithm.
Instruction
Multiply the number by 100. As an example, will be rounded to hundredths number 23,429. After multiplication by 100 are 2342,9.
Add the number to 0.5. In our case, the resulting 2343,4.
Take the integer part of the number. The digit after the decimal point swing, don't need it. Get the number 2343.
Divide the number by 100. In this step, you have 23,43 - this is the result of rounding to the hundredths.
Note
In the considered algorithm have the "thin place". He correctly rounds to hundredths number with three decimal places. For numbers with 4 digits after the decimal point at the 2nd step, it is necessary to increase the number of 0.55. For numbers with 5 characters - add number 0,555, etc., Then the result will be mathematically correct.
Useful advice
Using the algorithm, be sure to do the test calculations with different numbers. For example, you check the number 23,4276 - specified algorithm, with the addition on the 2nd step of 0.5. Everything works correctly. If for complacency, then errors will occur. After all, the number 23,4246 the check fails. When the rounding algorithm outputs the result of 23.42. Therefore, test calculations have different numbers.