我在C#中有这个号码
Double a = 1.2345678
我想要的是将它制成字符串后看起来像这样:
1.23456
答案 0 :(得分:2)
首先,将其转换为字符串。 例如,字符串定义为S。 然后应用此方法:
S.Remove(S.Length -2);
答案 1 :(得分:1)
您可以通过多种方式实现这一目标:
代码:
using System;
using System.Linq;
using System.Text;
namespace RemovingLastTwoNumber
{
class Program
{
static void Main(string[] args)
{
// Method 1.
double number = 1.2345678;
string numberInStringFormat = number.ToString();
string TargetNumber = numberInStringFormat.Substring(0, numberInStringFormat.Length - 2);
Console.WriteLine(number);
Console.WriteLine(TargetNumber);
// Method 2.
string _TargetNumber = Math.Round(number, 5).ToString();
Console.WriteLine(_TargetNumber);
// Method 3.
var characters = number.ToString().ToArray();
var __Characters = characters.Take(7);
StringBuilder __targetNumber = new StringBuilder();
foreach (var character in __Characters)
{
__targetNumber.Append(character);
}
Console.WriteLine(__targetNumber);
}
}
}