Peteroid wrote:
I want to use a Textbox to allow the user to input a floating point number.
In my case it will always be non-negative. Is there an easy way to do this,
or possibly a link to some sample code that shows how to do this?
As far as I know you will have to pass the input string yourself. However
Char::GetNumericValue() may provide much help, it converts a character digit to its
corresponding number value as a double.
For example
Char::GetNumericValue ('4'); returns 4 as a double.
So parsing string "54.145679" can be done like this:
#using <mscorlib.dll>
int main()
{
using namespace System;
String *s= "54.145679";
double d=0;
for(int i= s->Length-1; i>=0; --i)
{
static long nonDecimals= 0;
if(s->Chars[i]=='.')
{
nonDecimals=1;
continue;
}
if(!nonDecimals)
{
d+= Char::GetNumericValue(s->Chars[i]);
d/= 10;
}
else if(nonDecimals)
{
d+= nonDecimals*Char::GetNumericValue(s->Chars[i]);
nonDecimals*=10;
}
}
Console::WriteLine(d);
}
Of course you should validate the contents of the string to be sure it is a valid decimal
number before the conversion, or check the return value of Char::GetNumericValue() (-1.0
in case of error).