Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easiest way to check integer

Status
Not open for further replies.

cynikern

Programmer
Mar 22, 2003
3
US
how can I easy check if an ansistring can be an integer value?

i want the only input in a stringgrid cell to be 0-9.

i hope you could understand what i wanted to say, my english is not very good.

thanks for your help in advance
 
Howdy,

AnsiStrings have a ToFloat() method that you can use. Something like this will do the trick:

//Save the string as a float
float SomeVariable = TheString.ToFloat();

//Test if the float is a decimal or an int
if((int)SomeVariable == SomeVariable)
{
//Do whatever you want to do
}


This code takes your AnsiString (TheString) can saves it in SomeVariable as a floating point decimal, then tests if that float equals itself once typecasted as an integer.

Some AnsiStrings can't be evaluated for a float, and the only way I know to tell if a string can be evaluated as a float is to iterate through each character and test it. If you need me to do so I could post a sample...

Hope I could help,
onrdbandit
 

try
{
int x = MyString.ToInt();
float y = MyString.ToDouble();
// check valid range.
// set the database or whatever to new value.
}
catch (...)
{
// Code that says x wasn't an int or y wasn't a double.
}

Chris
 
thanks for all your help guys it solved my problem

//cynikern
 
try this too
on KeyDown or KeyPress event
if (((Key <'0') && (Key >'9')) || Key != 8)
Key = 0;

check the event OnChange too to prevent insertions from mouse



---LastCyborg---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top