【发布时间】:2021-06-18 21:16:48
【问题描述】:
我想在double? 的一个对象上使用 Math.Round 函数。我尝试转换为十进制,然后使用 math.round 函数,但收到此错误:
错误 CS0019:运算符“*”不能应用于“十进制”和“双精度”类型的操作数 (CS0019) (WeatherLocationInfo)
我的代码是:
double? wSpeedInputFirstDay = weatherBindingData.WeatherDataForecastHourly.List[0].WindForecast.WindForecastValue;
decimal wSpeedOutPutFirstDay = Convert.ToDecimal(wSpeedInputFirstDay);
string wSpeedFirstDay = $"{Math.Round(wSpeedOutPutFirstDay) * 3.6:0}km/h";
当我尝试直接将 Math.Round 与 double 一起使用时?像这样的对象:
double? wSpeedInputFirstDay = weatherBindingData.WeatherDataForecastHourly.List[0].WindForecast.WindForecastValue;
string wSpeedFirstDay = $"{Math.Round(wSpeedInputFirstDay) * 3.6:0}km/h";
我收到此错误:
错误 CS1503:参数 1:无法从“双精度”转换?到“十进制” (CS1503) (WeatherLocationInfo)
在double?对象上使用函数Math.Round的正确方法是什么?
【问题讨论】:
-
C# 仅对整数类型(
int等)实现对decimal的内置提升。如果您想在同一语句中对double和decimal执行数学运算,则必须显式转换。
标签: c#