C# 把长数除以10还是100?
我有两个字符串,想把它们分开。正如我所知,你不能分割字符串,你首先必须把它们转换成int,double,decimal,等等。现在,我的问题是字符串的长度是1000个字符或更多,所以我不能使用C# 把长数除以10还是100?,c#,string,type-conversion,C#,String,Type Conversion,我有两个字符串,想把它们分开。正如我所知,你不能分割字符串,你首先必须把它们转换成int,double,decimal,等等。现在,我的问题是字符串的长度是1000个字符或更多,所以我不能使用int(因为字符串太长,无法放入其中)。当我想将一个超过1000位的字符串除以10或100时,我能做什么 一种解决方案是使用支持任意长度的类型,例如 但是,当您想要除以10或100时,对于十进制数来说,解决方法要容易得多。除以10只不过是切掉最后一个字符;除以100是最后两个字符的切分。因此,如果您的输入是
int
(因为字符串太长,无法放入其中)。当我想将一个超过1000位的字符串除以10或100时,我能做什么 一种解决方案是使用支持任意长度的类型,例如
但是,当您想要除以10或100时,对于十进制数来说,解决方法要容易得多。除以10只不过是切掉最后一个字符;除以100是最后两个字符的切分。因此,如果您的输入是字符串,您可以使用来解决此问题:
string number = "124235235235"; // imagine a longer number here
// (integer) division by 10
Console.WriteLine(number.Substring(0, number.Length - 1)); // 12423523523
// (integer) division by 100
Console.WriteLine(number.Substring(0, number.Length - 2)); // 1242352352
如果需要除法,则必须找到小数点的索引,并将其向左移动一到两个位置:
string number = "123456.78901"; // imagine a longer number here
int i = number.IndexOf(".");
// division by 10
Console.WriteLine(number.Substring(0, i - 1) + "." + number.Substring(i - 1, 1) + number.Substring(i + 1)); // 12345.678901
// division by 100
Console.WriteLine(number.Substring(0, i - 2) + "." + number.Substring(i - 2, 1) + number.Substring(i + 1)); // 1234.5678901
一种解决方案是使用支持任意长度数的类型,例如 但是,当您想要除以10或100时,对于十进制数来说,解决方法要容易得多。除以10只不过是切掉最后一个字符;除以100是最后两个字符的切分。因此,如果您的输入是字符串,您可以使用来解决此问题:
string number = "124235235235"; // imagine a longer number here
// (integer) division by 10
Console.WriteLine(number.Substring(0, number.Length - 1)); // 12423523523
// (integer) division by 100
Console.WriteLine(number.Substring(0, number.Length - 2)); // 1242352352
如果需要除法,则必须找到小数点的索引,并将其向左移动一到两个位置:
string number = "123456.78901"; // imagine a longer number here
int i = number.IndexOf(".");
// division by 10
Console.WriteLine(number.Substring(0, i - 1) + "." + number.Substring(i - 1, 1) + number.Substring(i + 1)); // 12345.678901
// division by 100
Console.WriteLine(number.Substring(0, i - 2) + "." + number.Substring(i - 2, 1) + number.Substring(i + 1)); // 1234.5678901
一种解决方案是使用支持任意长度数的类型,例如 但是,当您想要除以10或100时,对于十进制数来说,解决方法要容易得多。除以10只不过是切掉最后一个字符;除以100是最后两个字符的切分。因此,如果您的输入是字符串,您可以使用来解决此问题:
string number = "124235235235"; // imagine a longer number here
// (integer) division by 10
Console.WriteLine(number.Substring(0, number.Length - 1)); // 12423523523
// (integer) division by 100
Console.WriteLine(number.Substring(0, number.Length - 2)); // 1242352352
如果需要除法,则必须找到小数点的索引,并将其向左移动一到两个位置:
string number = "123456.78901"; // imagine a longer number here
int i = number.IndexOf(".");
// division by 10
Console.WriteLine(number.Substring(0, i - 1) + "." + number.Substring(i - 1, 1) + number.Substring(i + 1)); // 12345.678901
// division by 100
Console.WriteLine(number.Substring(0, i - 2) + "." + number.Substring(i - 2, 1) + number.Substring(i + 1)); // 1234.5678901
一种解决方案是使用支持任意长度数的类型,例如 但是,当您想要除以10或100时,对于十进制数来说,解决方法要容易得多。除以10只不过是切掉最后一个字符;除以100是最后两个字符的切分。因此,如果您的输入是字符串,您可以使用来解决此问题:
string number = "124235235235"; // imagine a longer number here
// (integer) division by 10
Console.WriteLine(number.Substring(0, number.Length - 1)); // 12423523523
// (integer) division by 100
Console.WriteLine(number.Substring(0, number.Length - 2)); // 1242352352
如果需要除法,则必须找到小数点的索引,并将其向左移动一到两个位置:
string number = "123456.78901"; // imagine a longer number here
int i = number.IndexOf(".");
// division by 10
Console.WriteLine(number.Substring(0, i - 1) + "." + number.Substring(i - 1, 1) + number.Substring(i + 1)); // 12345.678901
// division by 100
Console.WriteLine(number.Substring(0, i - 2) + "." + number.Substring(i - 2, 1) + number.Substring(i + 1)); // 1234.5678901
可以使用表示任意大的有符号整数的
从MSDN:
BigInteger类型是表示
理论上其值没有上限或下限的任意大整数
界限。BigInteger类型的成员与
其他整数类型(字节、Int16、Int32、Int64、SByte、UInt16、,
UInt32和UInt64类型)。此类型不同于其他积分类型
.NET Framework中的类型,其范围由
MinValue和MaxValue属性
如果您满足以下两个条件,可以尝试以下方法:
注1:如果只想将其除以10或100。注意2:如果您想将结果作为字符串 试试这个:如果你不知道小数点
string num = "12345";
int index= num.IndexOf('.');
if(index>0)
num = num.Substring(0, index);
string dividedBy10 = num.Substring(0,num.Length - 1);//gives you 1234
string dividedBy100 = num.Substring(0, num.Length - 2);//gives you 123
试试这个:如果你想要小数点
string num = "12345";
int index= num.IndexOf('.');
if(index>0)
num = num.Substring(0,index);
string dividedBy10 = num.Insert(num.Length - 1, ".");//gives you 1234.5
string dividedBy100 = num.Insert(num.Length - 2, ".");//gives you 123.45
可以使用表示任意大的有符号整数的
从MSDN:
BigInteger类型是表示
理论上其值没有上限或下限的任意大整数
界限。BigInteger类型的成员与
其他整数类型(字节、Int16、Int32、Int64、SByte、UInt16、,
UInt32和UInt64类型)。此类型不同于其他积分类型
.NET Framework中的类型,其范围由
MinValue和MaxValue属性
如果您满足以下两个条件,可以尝试以下方法:
注1:如果只想将其除以10或100。注意2:如果您想将结果作为字符串 试试这个:如果你不知道小数点
string num = "12345";
int index= num.IndexOf('.');
if(index>0)
num = num.Substring(0, index);
string dividedBy10 = num.Substring(0,num.Length - 1);//gives you 1234
string dividedBy100 = num.Substring(0, num.Length - 2);//gives you 123
试试这个:如果你想要小数点
string num = "12345";
int index= num.IndexOf('.');
if(index>0)
num = num.Substring(0,index);
string dividedBy10 = num.Insert(num.Length - 1, ".");//gives you 1234.5
string dividedBy100 = num.Insert(num.Length - 2, ".");//gives you 123.45
可以使用表示任意大的有符号整数的
从MSDN:
BigInteger类型是表示
理论上其值没有上限或下限的任意大整数
界限。BigInteger类型的成员与
其他整数类型(字节、Int16、Int32、Int64、SByte、UInt16、,
UInt32和UInt64类型)。此类型不同于其他积分类型
.NET Framework中的类型,其范围由
MinValue和MaxValue属性
如果您满足以下两个条件,可以尝试以下方法:
注1:如果只想将其除以10或100。注意2:如果您想将结果作为字符串 试试这个:如果你不知道小数点
string num = "12345";
int index= num.IndexOf('.');
if(index>0)
num = num.Substring(0, index);
string dividedBy10 = num.Substring(0,num.Length - 1);//gives you 1234
string dividedBy100 = num.Substring(0, num.Length - 2);//gives you 123
试试这个:如果你想要小数点
string num = "12345";
int index= num.IndexOf('.');
if(index>0)
num = num.Substring(0,index);
string dividedBy10 = num.Insert(num.Length - 1, ".");//gives you 1234.5
string dividedBy100 = num.Insert(num.Length - 2, ".");//gives you 123.45
可以使用表示任意大的有符号整数的
从MSDN:
BigInteger类型是表示
理论上其值没有上限或下限的任意大整数
界限。BigInteger类型的成员与
其他整数类型(字节、Int16、Int32、Int64、SByte、UInt16、,
UInt32和UInt64类型)。此类型不同于其他积分类型
.NET Framework中的类型,其范围由
MinValue和MaxValue属性
如果您满足以下两个条件,可以尝试以下方法:
注1:如果只想将其除以10或100。注意2:如果您想将结果作为字符串 试试这个:如果你不知道小数点
string num = "12345";
int index= num.IndexOf('.');
if(index>0)
num = num.Substring(0, index);
string dividedBy10 = num.Substring(0,num.Length - 1);//gives you 1234
string dividedBy100 = num.Substring(0, num.Length - 2);//gives you 123
试试这个:如果你想要的话