Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用十进制阈值将浮点值向上舍入为int作为字符串_C#_Unity3d - Fatal编程技术网

C# 使用十进制阈值将浮点值向上舍入为int作为字符串

C# 使用十进制阈值将浮点值向上舍入为int作为字符串,c#,unity3d,C#,Unity3d,我在计算变量中的浮点时间,然后将浮点转换为2个小数点,然后将其传递给字符串变量。但是,我不需要将浮点值转换为小数点,而是将浮点值四舍五入为整数。我如何做到这一点 也就是说,如果计时器>26.50f,则最终计时器应为27 另外,我们可以手动设置阈值吗?阈值是小数点后的值吗?然后脚本决定整数属于哪个数字? 例如,我将阈值设置为.25到.99 float 1 = 23.26 = 24 float 2 = 17.25 = 18 float 3 = 19.24 = 19 将下一个整数四舍五入称为“上限”

我在计算变量中的浮点时间,然后将浮点转换为2个小数点,然后将其传递给字符串变量。但是,我不需要将浮点值转换为小数点,而是将浮点值四舍五入为整数。我如何做到这一点

也就是说,如果计时器>26.50f,则最终计时器应为27

另外,我们可以手动设置阈值吗?阈值是小数点后的值吗?然后脚本决定整数属于哪个数字? 例如,我将阈值设置为.25到.99

float 1 = 23.26 = 24
float 2 = 17.25 = 18
float 3 = 19.24 = 19

将下一个整数四舍五入称为“上限”,即
Math.天花
。所以我怀疑你想要的是:

timer\u string=Math.天花(timer.ToString)(…);
(供您选择格式


如果您实际上是指其他形式的舍入,那么请使用
Math.Round(timer,middpointrounding.yourchehere)
而不是
Math.Ceiling
;例如,<>代码> AWAYAVEL0<,<代码> ToposisithEnimuly等

< P>一种考虑的方法是利用现有的<代码>数学>圆< /代码>(中点舍入<代码> AWAYAVENRO0),但有效地转移点是舍入(加法/减法)。这看起来像:

static double RoundBasedOnCustomThreshold(double number, double customThreshold = 0.25)
{
    // customThreshold of 1 will be equivalent to Math.Floor
    if (customThreshold <= 0 || customThreshold > 1)
        throw new ArgumentException();

    return Math.Round(number + 0.5 - customThreshold, 0, MidpointRounding.AwayFromZero);
}
静态双RoundBasedOnCustomThreshold(双倍数字,双倍customThreshold=0.25)
{
//customThreshold为1将相当于Math.Floor
如果(自定义阈值1)
抛出新ArgumentException();
返回Math.Round(数字+0.5-自定义阈值,0,中点舍入。远离零);
}
通过添加
0.5-customThreshold
,这可能会以您所寻找的方式进行汇总


你可以在上玩一些输入值和结果。

Mathf.Ceil()
是为了统一,这是为了取整吗?@mjwills这是不明确的,我同意-但OP明确表示至少“取整”twice@MarcGravell如果值大于0.5f,则使用
Mathf.Ceil()
的函数可以工作,然后下一个值被选为整数。@HimBromBeere我做了,我想知道我们是否可以手动设置它。这是默认值0.5f,但我们可以设置不同的值吗?@Saif这是什么意思?有什么不同的价值?如果使用一个不是0.5的示例,这可能会更好?或者是一些不同的例子,尽管这仍然不能满足像OP所说的那样定制取整阈值的要求,例如,ceil值已经超过
.25
,而不是
.5
(在这种特定情况下,可以通过先乘以10,ceil,然后再除以10来解决)。。对于更一般的用例,它可能会变得很棘手,例如,在
.237
上取整。。我想,要让它在一个通用阈值下工作是相当棘手的?是的,实际上我也考虑过同样的方法,添加
0.5-threshold
,然后取而代之。(但这是睡觉前5分钟:D)
static double RoundBasedOnCustomThreshold(double number, double customThreshold = 0.25)
{
    // customThreshold of 1 will be equivalent to Math.Floor
    if (customThreshold <= 0 || customThreshold > 1)
        throw new ArgumentException();

    return Math.Round(number + 0.5 - customThreshold, 0, MidpointRounding.AwayFromZero);
}