什么时候使用Decimal 类型(When to Use the Decimal Type)

在货币或者财务方面的计算的时候建议使用decimal (System.Decimal)类型,或者在你需要使用很多位小数以及高精度的时候应该使用decimal 类型,以避免舍入方面的错误。

比如:

float f1 = 12345678901234567.28f;     // 1.23456784E+16
double d1 = 12345678901234567.28d;    // 12345678901234568.0
decimal dc1 = 12345678901234567.28m;  // 12345678901234567.28

上面的例子中float和double类型都由于精度不,对所存储的数进行了舍弃。

decimal 类型提供更高的精度,但是它所能表示的数据范围却并不比 float 或者 double类型高,这是因为它将更多的位数用于存储小数位。

float – 4 bytes  (±1.5e−45 to ±3.4e38, 7 位精度)
double – 8 bytes  (±5.0e−324 to ±1.7e308, 位精度)
decimal – 16 bytes  (±1.0 × 10−28 to ±7.9 × 1028, 28-29 位精度)

原文地址:https://csharp.2000things.com/2010/08/05/49-when-to-use-the-decimal-type/

********************************************译者注**************************************

decimal 类型是 128 位数据类型。同浮点型相比,decimal 类型具有更高的精度和更小的范围,这使它适合于财务和货币计算。如果希望实数被视为 decimal 类型,请使用后缀 m 或 M。

decimal myMoney = 300.5m;
如果,没有m。数字将会被视为double类型,从而导致编译错误。


there is a file,use c write a System Requirements: A. To make the program as user-friendly as possible, use the following menu to start. 1. Import grades for a course 2. Delete a course 3. Calculate GPA 4. Export file with GPA 5. Quit User will choose the functions using the index number: 1: ask user to import a file from which the grades will be added to students.txt. a) The program should ask the user to input the course name before input the file name. It is assumed that the length of course name is less than 20. b) If the course name inputted by user exists in students.txt, gives user a warning. User can choose to replace the old grades with new or not. If user choose not to replace, go back to main menu. c) If the course name does not exist in students.txt, ask the user to give the file name to import. d) If it is found that a student ID in this imported file cannot be found in the students.txt, do not import and give the warning. Each ID contains 10 digits. It is assumed that each file has been sorted according to ID in ascending order before it is imported. 2: ask user to input the course name to delete. If the course name does not exist in students.txt, gives a warning. 3: the system will calculate GPA for each student in students.txt and put the GPA in the file too. Keep two decimal points in GPA. 4: when user has this choice, the system will display a sub-menu. 1. Sorted in ID (ascending) 2. Sorted in GPA (descending) 3. Back to main menu In this sub-menu, if user chooses 1 or 2, the system will ask user to input a file name to export to. If user chooses 3, system goes back to main menu. 5: Quit the system.
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值