0

I'm trying to use strings to do math with very large numbers using strings, and without external libraries.

I have tried looking online with no success, and I need functions for addition, subtraction, multiplication, and division (if possible, and limited to a specified number of decimal places.)

example: add 9,900,000,000 and 100,000,020 should be 10,000,000,020.

EDIT: Im sorry I diddn't be specific enough, but I can only use Strings. no Long, bigInt, anything. just the basic string and if nessecary, int32.

This is NOT a homework question!

6
  • 1
    Int64 will do that. You dont even need a 'big number'. Commented Nov 8, 2011 at 3:43
  • and limited to a specified number of decimal places. do you need support for decimals at all? Otherwise long and BigInteger are out. Commented Nov 8, 2011 at 3:45
  • 1
    Why are you rejecting BigInt? "Hi, I have a problem, and object X was designed to solve exactly this problem. How can I solve the problem without using X?" Commented Nov 8, 2011 at 5:40
  • 1
    @RaymondChen: It's probably a homework problem. lpquarx: Is it a homework problem? If so, explain what you have tried so far and where you are stuck. Can you solve the problem in strings to do, say, addition of two-digit numbers? Commented Nov 8, 2011 at 6:24
  • 4
    The restrictions you specify are unusual in a real environment. Perhaps people would be more inclined to help if they understood why you can't use BigInteger in this context. Commented Nov 8, 2011 at 16:22

3 Answers 3

7

Have you looked at BigInteger ?

Sign up to request clarification or add additional context in comments.

Comments

1

If you're using .NET Framework 4, you can make use of the new System.Numerics.BigInteger class, which is an integer that can hold any whole number at all, until you run out of memory.

(The examples you provide, by the way, can be computed using long or System.UInt64.)

Comments

-1

You have to convert the value in bits first & then apply the operation which you wish. After operation, you should convert back the bits to the number.

1 Comment

Are you talking about converting to string of bits? Because "I can only use Strings. no Long, bigInt, anything. just the basic string and if nessecary, int32."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.