C#DoubleToFloat不四舍五入
程序员文章站
2022-07-12 15:46:47
...
//Ctrl+K,C: 注释选定内容
//Ctrl+K,U: 取消选定注释内容
//手动四舍五入
float n=0.3;
n+=0.5;
int m=math.floor(n);
//暴击问题
num=80;
r=round(0,100);
if(r>=num)
{
暴击;
}
//正文
static void Main(string[] args)
{
double a = 2.123456789;
//double a = 2.789;
//double a = 2.14;
//double a = 2;
Console.WriteLine(ToFlaot(a));
}
static float ToFlaot(double num)
{
string str = num.ToString();
if(str.Contains("."))
{
str = str.Insert(str.Length, "0000000");
Console.WriteLine(str);
str = str.Substring(0, str.IndexOf(".", 0) + 7);
}
return float.Parse(str);
}