asp.net - 在 ASP.NET 中使用按钮进行计算
问题描述
我正在尝试做一个超级简单的界面,用户可以在其中输入他们的本金金额、年利率和贷款期限。每月付款、付款总额、支付利息总额的计算字段。我正在计算,但不断出现错误,并且似乎小数位在整个计算过程中没有被保留?
Protected Sub btncalculate_Click(sender As Object, e As EventArgs) Handles btncalculate.Click
Dim n As Integer
Dim P As Integer
Dim yr As Integer
Dim ra As Integer
Dim r As Integer
n = CInt(txtmonths.Text)
P = CInt(txtprinciple.Text)
yr = CInt(txtyearlypercent.Text)
ra = (yr / 100) / 12
r = (P * ra) / (1 - (1 / (1 + ra) ^ n))
lblmonpayments.Text = r.ToString("c")
End Sub
解决方案
我怀疑错误信息是Arithmetic operation resulted in an overflow
?
您收到此错误的原因是因为ra
和r
都被分配为Integer
. 但是,这些用于使用除法的计算。结果很可能会返回十进制数而不是整数。只需更改要使用的变量Decimal
Protected Sub btncalculate_Click(sender As Object, e As EventArgs) Handles btncalculate.Click
Dim n As Integer
Dim P As Integer
Dim yr As Integer
Dim ra As Decimal
Dim r As Decimal
n = CInt(txtmonths.Text)
P = CInt(txtprinciple.Text)
yr = CInt(txtyearlypercent.Text)
ra = (yr / 100) / 12
r = (P * ra) / (1 - (1 / (1 + ra) ^ n))
lblmonpayments.Text = r.ToString("c")
End Sub
推荐阅读
- arrays - 将一维字符串转换为多维数组 - Numpy
- html - iFrame 不显示网站(跨站请求错误)
- java - 如何在对象列表中查找某些字段的重复项?
- typescript - 应该如何构建打算通过 git url 安装的 Typescript 库?
- java - 如何连接字符而不是添加数值
- javascript - 在谷歌电子表格中值从 TRUE 变为 FALSE 后,有没有办法停止 COUNTIF 函数?
- google-cloud-pubsub - DataflowTemplates debezium 连接器问题
- scikit-learn - 评估缩放的 RMSE
- javascript - Jquery mousedown 没有在数据表上触发
- c++ - Painter 在继承的 QOpenGLWidget 类上不活动