【发布时间】:2012-11-30 15:58:48
【问题描述】:
我目前正在从字符串中读取一个长数字。该数字是 3 个整数位,后跟 7 个不带小数点的小数位(例如 1234567890)。
如何在解析前除掉这个数字?
我正在尝试将其解析为整数,但整数的最大值约为 20 亿。
这是我尝试过的:
class Program
{
static void Main(string[] args)
{
using (StreamReader reader = new StreamReader("CLIFF.dat"))
{
string line;
var locations = new Dictionary<string, int[]>() {
{"210", new [] {405, 4, 128, 12, 141, 12, 247, 15}},
{"310", new [] {321, 4, 112, 12, 125, 12, 230, 15}}, //
{"410", new [] {477, 4, 112, 12, 125, 12, 360, 15 }}
};
while ((line = reader.ReadLine()) != null)
{
var lineStart = line.Substring(0, 3);
if (lineStart == "210" || lineStart == "310" || lineStart == "410")
{
var currentLocations = locations[lineStart];
var letters = line.Substring(currentLocations[0], currentLocations[1]);
var transactionvolume =
int.Parse(line.Substring(currentLocations[2], currentLocations[3])) +
int.Parse(line.Substring(currentLocations[4], currentLocations[5]));
var watching = line.Substring(currentLocations[6], currentLocations[7]);
var number = int.Parse(line.Substring(currentLocations[6], currentLocations[7])*));
是当前位置[6]数太大,需要乘以10^-7。
【问题讨论】:
-
使用 long.Parse 然后除法。
-
BigInteger 然后除,也许?
-
两个都试一下,看看我更喜欢哪个,谢谢。
-
@Cylen 好吧,我认为这主要取决于您的数字到底有多大......
-
或者,使用decimal.Parse从字符串构建一个小数。既然你知道小数点放在哪里,你可以做一些类似decimal.Parse(firstPart + "." + FractionalPart)等的事情。