Reputation: 161
I'm trying to create a calculator where you have two text boxes for numeric inputs and the last one which displays the number. However, i want to make it such that it will go to at least 80 digits without scientific notation(decimals don't matter). Does anyone have any suggestions as to how I would implement this ?
Thank you.
public void button1_Click(object sender, EventArgs e)
{
//Addition
**resultAdd = Convert.ToDouble(tb1.Text) ;**
tb2.Enabled = true;
calcFunc = "Add";
}
private void btnSubtract_Click(object sender, EventArgs e)
{
//subtract
resultSubtract = Convert.ToDouble(tb1.Text) + 0;
tb2.Enabled = true;
calcFunc = "Subtract";
}
public void btnEqual_Click(object sender, EventArgs e)
{
double totalResult;
switch (calcFunc)
{
case "Add":
totalResult = Convert.ToDouble(tb2.Text) + resultAdd;
total = tbTotal.Text.ToCharArray();
string x = Convert.ToString(totalResult);
tbTotal.Text = x;
// tbTotal.Text = x;
tbTotal.Enabled = true;
**totvalue = BigInteger.Parse(x);**
Upvotes: 1
Views: 1791
Reputation: 1500575
If by "decimals don't matter" you mean you're only dealing with integers, and assuming your using .NET 4 or higher, then you should use System.Numerics.BigInteger
which allows for arbitrarily large integers.
(You'll still need to do all the GUI work, but you can use BigInteger
for all the calculations.)
Short but complete example to show that BigInteger
is perfectly capable of handling large numbers:
using System;
using System.Numerics;
class Test
{
static void Main()
{
string text = new string('1', 80);
BigInteger number = BigInteger.Parse(text);
Console.WriteLine(number);
}
}
EDIT: Okay, now you've posted your code, it's obvious what's wrong - you're going via double
for no reason in this code:
totalResult = Convert.ToDouble(tb2.Text) + resultAdd;
total = tbTotal.Text.ToCharArray();
string x = Convert.ToString(totalResult);
tbTotal.Text = x;
totvalue = BigInteger.Parse(x);
You're not trying to handle double
values - you're trying to handle integers so do everything with BigInteger
. Change resultAdd
to a BigInteger
too, and then all you need is:
BigInteger result = BigInteger.Parse(tb2.Text) + resultAdd;
No need to do any other string conversions.
Upvotes: 12