C# 检查字符串值并将其四舍五入到最接近的Int16的最佳(最小)方法是什么
我试图检查字符串值(response.Radius)并将其四舍五入到最接近的Int16值(Radius)。最干净或最有效的方法是什么?我已经编写了以下代码,并发现这是最有效的解决方案。我说得对吗 还有一些额外的日志信息存储在catch语句中C# 检查字符串值并将其四舍五入到最接近的Int16的最佳(最小)方法是什么,c#,C#,我试图检查字符串值(response.Radius)并将其四舍五入到最接近的Int16值(Radius)。最干净或最有效的方法是什么?我已经编写了以下代码,并发现这是最有效的解决方案。我说得对吗 还有一些额外的日志信息存储在catch语句中 Int16 radius; Double rDouble; if (Double.TryParse(response.Radius, out rDouble)) { var rR
Int16 radius; Double rDouble;
if (Double.TryParse(response.Radius, out rDouble))
{
var rRounded = Math.Round(rDouble);
if (!Int16.TryParse(rRounded.ToString(), out radius))
{
if (rRounded > Int16.MaxValue)
{
radius = Int16.MaxValue;
}
else if (rRounded < Int16.MinValue)
{
radius = Int16.MinValue;
}
//response.Radius = radius.ToString();
Logger.Info(String.Format("Received range value {0} is outside the range of SmallInt, thus it is capped to nearest value of SmallInt i.e. {2}", Int16.MaxValue, response.Radius));
}
else
{
Logger.Info("Response: Range " + response.Radius + " is not a valid number");
}
}
return response.Radius;
Int16半径;双倍;
if(双锥棱镜(response.Radius,out-rDouble))
{
var rRounded=数学四舍五入(rDouble);
如果(!Int16.TryParse(rounded.ToString(),out radius))
{
如果(rRounded>Int16.MaxValue)
{
半径=Int16.MaxValue;
}
else if(rRounded
我不知道这是否是“最好”的方法(可能不是),但它应该更快(通过不使用异常)并且更不容易出错
public static Int16? ToInt16(string value)
{
double rDouble;
if (!double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out rDouble))
{
// log: not a valid number
return null;
}
if (rDouble < Int16.MinValue)
{
// log: too small
return Int16.MinValue;
}
if (rDouble > Int16.MaxValue)
{
// log: too big
return Int16.MaxValue;
}
var rounded = Math.Round(rDouble, MidpointRounding.AwayFromZero);
return (Int16)rounded;
}
publicstaticint16?ToInt16(字符串值)
{
双倍;
if(!double.TryParse(value,NumberStyles.Any,CultureInfo.InvariantCulture,out-rDouble))
{
//日志:不是有效的数字
返回null;
}
if(rDoubleInt16.MaxValue)
{
//圆木:太大了
返回Int16.MaxValue;
}
var rounded=数学圆整(rDouble,中点Rounding.AwayFromZero);
返回(Int16)四舍五入;
}
此方法将返回一个(Nullable
public static Int16? ToInt16(string value)
{
double rDouble;
if (!double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out rDouble))
{
// log: not a valid number
return null;
}
if (rDouble < Int16.MinValue)
{
// log: too small
return Int16.MinValue;
}
if (rDouble > Int16.MaxValue)
{
// log: too big
return Int16.MaxValue;
}
var rounded = Math.Round(rDouble, MidpointRounding.AwayFromZero);
return (Int16)rounded;
}
publicstaticint16?ToInt16(字符串值)
{
双倍;
if(!double.TryParse(value,NumberStyles.Any,CultureInfo.InvariantCulture,out-rDouble))
{
//日志:不是有效的数字
返回null;
}
if(rDoubleInt16.MaxValue)
{
//圆木:太大了
返回Int16.MaxValue;
}
var rounded=数学圆整(rDouble,中点Rounding.AwayFromZero);
返回(Int16)四舍五入;
}
此方法将返回一个(Nullable
Math.Min
和Math.Max
:
double d = 42793.5;
double rslt = Math.Min(Int16.MaxValue, Math.Max(Int16.MinValue, d));
如果您想要更小的代码,可以使用
Math.Min
和Math.Max
:
double d = 42793.5;
double rslt = Math.Min(Int16.MaxValue, Math.Max(Int16.MinValue, d));
这是我能写的最小且准确的代码
Double rDouble;
if (Double.TryParse(response.Radius, out rDouble))
{
var radius = Math.Round(Math.Min(Int16.MaxValue, Math.Max(Int16.MinValue, rDouble)));
if (radius.ToString() != response.Radius))
{
Logger.Info(String.Format("Response: Received range value {0} is outside the range of SmallInt, thus it is capped to nearest value of SmallInt i.e. {1}", response.Radius, radius));
}
response.Radius = radius.ToString();
}
else
{
Logger.Info("Response: Range " + response.Radius + " is not a valid number");
}
这是我能写的最小且准确的代码
Double rDouble;
if (Double.TryParse(response.Radius, out rDouble))
{
var radius = Math.Round(Math.Min(Int16.MaxValue, Math.Max(Int16.MinValue, rDouble)));
if (radius.ToString() != response.Radius))
{
Logger.Info(String.Format("Response: Received range value {0} is outside the range of SmallInt, thus it is capped to nearest value of SmallInt i.e. {1}", response.Radius, radius));
}
response.Radius = radius.ToString();
}
else
{
Logger.Info("Response: Range " + response.Radius + " is not a valid number");
}
Offtopic更适合于在抛出异常时为什么要使用
TryParse
?此外,您使用异常来控制逻辑流,这是一个坏主意。rRounded是十进制或双精度,您正在将其传递给Int16.TryParse()。那会编译吗?接下来,十进制分隔符呢?在某些区域性中,“1.0”
将被解析为10
;所以最好使用double.TryParse(response.Radius,NumberStyles.Any,CultureInfo.InvariantCulture,out-rDouble)
。另外,Math.Round(1.5)
和Math.Round(2.5)
都会产生2.0
,因为使用了四舍五入。如果您不想这样做,您可能应该使用Math.Round(rDouble,MidpointRounding.AwayFromZero)
。我没有编译就编写了它,只需将它更改为可编译版本。Offtopic更适合于在抛出异常时为什么要使用TryParse
?此外,您使用异常来控制逻辑流,这是一个坏主意。rRounded是十进制或双精度,您正在将其传递给Int16.TryParse()。那会编译吗?接下来,十进制分隔符呢?在某些区域性中,“1.0”
将被解析为10
;所以最好使用double.TryParse(response.Radius,NumberStyles.Any,CultureInfo.InvariantCulture,out-rDouble)
。另外,Math.Round(1.5)
和Math.Round(2.5)
都会产生2.0
,因为使用了四舍五入。如果您不想这样做,您可能应该使用Math.Round(rDouble,middpointrounding.AwayFromZero)
。我没有编译就编写了它,只需将它更改为可编译版本。您的smallercode很完美,但我需要将它从字符串转换为,如果超出smallint的范围,也要记录,然后转换为最接近的smallint。@ImranRizvi:您已经有代码来进行转换和范围检查。我的代码只是替换了强制它进入范围的代码。(也就是说,你的两个if
语句)只要记住,如果“较小的代码”降低了代码的可读性,那么它并不总是最好的解决方案。@Teknikaali:你完全正确。对于一些人来说,min/max是一个常见的习惯用法,因此对于那些人来说,它实际上提高了可读性。但是当你第一次看到它的时候,你必须抓挠你的头。我想这取决于你的观众。不过,您可以将它分成两个单独的调用(一个调用Math.Min
,另一个调用Math.Max
)。这将提高可读性,并且