AnsiString to number? conversion problems (using borland builder)

May 11, 2014 at 4:57pm
I am having a world of problems of what i thought to be rather simple.

Edit1->Text = "123401278034987120478102873401982734";

Now Edit1->Text is an AnsiString

and i would like to format this to a number value, so that i can say.

CSpinEdit1->Value = "123401278034987120478102873401982734"

In other words transferring the value from Edit1->Text to CpinEdit1->Value;

Integers won't work because the number is to big. So i tried going from String to Number like in this article
Converting numbers to strings and string - C++ Articles
http://www.cplusplus.com/articles/D9j2Nwbp/



But that won't work because its an Ansistring, how would i go about this conversion?
May 11, 2014 at 8:14pm
As you correctly said, the value is too big for an integer. It really depends on what you are intending to do with the resulting number. There is no built-in type which can handle an integer with that many digits. One possibility is to use a type double, which will give an approximation.
1
2
    AnsiString a = "123401278034987120478102873401982734";
    double d  = a.ToDouble();

The variable d will have the value of about 1.23401278034987e+35 (using scientific notation).

If you really need the full precision you will need to use some external bignum library.

edit Sorry, I skipped past the CSpinEdit. I've not used that and am not familiar with it. It looks like what you are attempting to do is not possible with TCSpinEdit and a number as large as that. The problem is not in the conversion, but in the TCSpinEdit component itself, it isn't suitable for use with a number having that many digits as the TCSpinEdit->Value is a long int.

One other comment. It probably won't help here, but it's useful to know. To convert an AnsiString to an ordinary c-string, use the .c_str() member function. Example:
1
2
    AnsiString a =  "12348765";
    int x = atoi(a.c_str());
Last edited on May 12, 2014 at 11:49am
Topic archived. No new replies allowed.