gpt4 book ai didi

c# - 从两个单独的变量 C# 创建 double 值

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:02 25 4
gpt4 key购买 nike

我有两个整数类型的独立变量 x 和 y

假设 x = 123 和 y = 456。我想使用这两个变量创建一个 double 值,这样结果 = 123.456。

我如何得到这个?

最佳答案

public static double Combine(int x, int y)
{
if (x < 0 || y < 0) throw new NotSupportedException(); // need to specify
// how it should behave when x or y is below 0
if (y == 0) return x;

var fractionMultipler = (int)Math.Floor(Math.Log10(y)) + 1;
var divider = Math.Pow(10, fractionMultipler);


return x + (y / divider);
}

示例:

 var z = Combine(30, 11123); // result 30.11123

关于c# - 从两个单独的变量 C# 创建 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27312651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com