# Round the number to two decimal places

Source: Internet
Author: User

Comment: Rounding the number to retain two decimal places

Aket 6799 0:46:18
It is not necessary to rounding the number into and keep two decimal places. Use the following code.
To directly use the roundto function, you must add math to uses.
Roundto (1.245,-2); = 1.25

It should be noted that the old Delphi version of the round function uses four homes and six inputs. In every five, the first entry is an odd number, and the last entry is an even number, examples in the Delphi manual help are as follows:
Roundto (1234567, 3) 1234000
Roundto (1.234,-2) 1.23
Roundto (1.235,-2) 1.24
Roundto (1.245,-2) 1.24

But Delphi 7 is not like this anymore. It is rounded down directly: roundto (1.245,-2) = 1.25.
To be compatible with the old version, you can process the code, add 0.005, and use the round function, as shown in the preceding example:
Roundto (1.245 + 0.005,-2 );
Aket 6800 0:50:50
The above roundto (1.245 + 0.005,-2); error should be handled as follows:
Function sswr (r: Real): real;
Begin
Result: = r * 100 + 0.5;
Result: = trunc (result)/100;
End;
Iamdream 6810 15:05:33
Here are the results of my study (DELPHI6 ):
The roundto (and round) function uses the so-called "banker Rounding Method" <banker's rounding> (if the number of rounded bits is 5, the number of rounded bits is even, if it is an odd number, it is input), and the format function is a normal rounding method, take the following:
0.145 roundto (0.145,-2) = 0.14
0.145 format ('% 8.2f', 0.145) = 0.15
0.155 roundto (0.155,-2) = 0.16
0.155 format ('% 8.2f', 0.155) = 0.16
In addition, 0.14499999999999 may occur during Floating Point calculation. In this case, the value should actually be 0.145 according to mathematical requirements. If you use the above method to round to the second decimal point, can not achieve the desired effect.
In addition, simpleroundto is closer to the rounding method than roundto, but not completely rounding. For example, if simpleroundto (0.145,-2) is used for 0.145, it is still 0.14;
If there are decimal places, such as 0.1450000001, then round, all three are 0.15!
For 1.145, simpleroundto is rounded to 1.15!
Therefore, if roundto is used for rounding, you can add 5 to the second place after the rounding, for example, 0.145, and 0.0005. Then it becomes 0.1455. When the roundto is used, it is 0.15!
Therefore, when rounding two decimal places, you only need to add 0.0001 first and then use roundto or simpleroundto.
<2004.7.15>
Iamdream 6811 15:09:50
Therefore, if roundto is used for rounding, you can add 5 to the second place after the rounding, for example, 0.145 to 0.0005. At this time, it becomes 0.1455, it's 0.15!" Should be deleted.
Test004 6818 16:35:27
It's really good. I don't think it makes sense.
Aket 6821 18:28:43
A problem was found in the test yesterday, as shown below:
Function sswr (r: Real): real;
Begin
Result: = r * 100 + 0.5;
Result: = trunc (result)/100;
End;
And
Function sswr (r: Real): real;
Begin
Result: = trunc (R * 100 + 0.5)/100;
End;
If the results are different, only four homes and six homes are allowed. Why?
Suexy 7564 13:12:00
You only need format ('%. 2f', [floattostr (yourfloat)] to get the last two digits of the decimal point and rounding them.
Shaoyy 7632 3:39:16
Why is it so troublesome to implement this function? In fact, you can simply implement it without using roundto:

F: = trunc (F + 0.005) * 100)/100
Wataki 7694 3:13:10
F: = trunc (F + 0.005) * 100)/100
May Overflow
Shaoyy 7713 14:40:14
Overflow does not need to be considered. This kind of operation is very common. In reality, it is almost impossible to find a real-type number multiplied by 100.
Wr960204 7886 18:03:46
Uses
Math;
The simpleroundto function is rounded.
Blazingfire 7888 19:43:02
These lines of code are not public. Inefficient
In fact, it is easy to achieve this function.
Qian8233 7911 2004-8-19 18:34:15
We can understand this situation because we have little knowledge about system functions;
The roundto function in Delphi7 is helpful: "roundto (1234567, 3) = 1234000" is completely incorrect, and the actual running result is exactly the same as our understanding of rounding in elementary school: 1235000.
Web700 11867 11:39:36
Function intdata (D: Double): Double;
Begin
Result: = int (D + 0.5) * 100/100 );
End;
Tinysoft 12854 21:09:13
After reading the methods, I still like to use the format function!
Gz818 21140 17:43:52
In Delphi7, how does one turn six into five single-doubles?

Minute: (39281533) 15:06:48
Type
Troundtorange =-37... 37;

Function myroundto (const avalue: Double; const adigit: troundtorange): Double;
VaR
Lfactor: Double;
Begin
Lfactor: = intpower (10, adigit );
Result: = trunc (avalue/lfactor + 0.5) * lfactor;
End;
Minute: (39281533) 15:06:57
However, the math unit must be referenced.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.