我必须在 SQL Server 2005 中设置小数点后 3 位。我只能从模型部分设置它。但在 sql server 中设置的 Salary 数据类型是小数。
当我设置喜欢
public decimal Salary { get; set; }
它想将数据库保存为 21000.000 而不是 21000。如果我输入 30000.75,那么它将保存到 30000.750。
我必须在 SQL Server 2005 中设置小数点后 3 位。我只能从模型部分设置它。但在 sql server 中设置的 Salary 数据类型是小数。
当我设置喜欢
public decimal Salary { get; set; }
它想将数据库保存为 21000.000 而不是 21000。如果我输入 30000.75,那么它将保存到 30000.750。
The easiest way to solve your problem is to set the Salary
datatype to decimal(18, 3)
. So even if you insert a value 30000.75123
only the first three decimal digits will be saved - 30000.750
. If you insert a whole number 21000
then the value in the database will be 21000.000
.
Even me faced this problem , one thing you can do with before sending to the database you will have to convert the value into 3 decimal places . you can do this by -
txtsalary.Text = String.Format("{0:0.000}", Salary);
or
decimal Salary ;
if (Decimal.TryParse(txtsalary.Text, out Salary )) // Returns true on valid input, on top of converting your string to decimal.
{
// ... code that is doing stuff with the decimal value ...
txtsalary.Text = String.Format("{0:0.000}", Salary );
}
else
{
// do your error handling here.
}