Skip to main content
 首页 » 编程设计

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

2024年08月12日23lyj

我有两个整数类型的独立变量 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