Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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/7/sqlite/3.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# 如何使用string.Format()来截断而不是取整?_C#_Rounding_String.format - Fatal编程技术网

C# 如何使用string.Format()来截断而不是取整?

C# 如何使用string.Format()来截断而不是取整?,c#,rounding,string.format,C#,Rounding,String.format,我希望应用string.Format(),使其执行截断而不是舍入。比如说 string.Format("##", 46.5); // output is 47, but I want 46 string.Format("##.#", 46.55); // output is 46.6, but I want 46.5 使用Math.Truncate在格式化输出之前进行截断这是“Truncate”(带n),BTW我认为这不是他想要的46.55为46.5。Math.Truncate将返回46.va

我希望应用
string.Format()
,使其执行截断而不是舍入。比如说

string.Format("##", 46.5); // output is 47, but I want 46
string.Format("##.#", 46.55); // output is 46.6, but I want 46.5

使用
Math.Truncate
在格式化输出之前进行截断

这是“Truncate”(带n),BTW我认为这不是他想要的46.55为46.5。Math.Truncate将返回46.var v=Math.Truncate(46.5);v、 ToString(“##.##”)给出46此答案不使用字符串格式来获得所需结果。