Ecere SDK/eC Forums • View topic - Numeric check




Ecere SDK/eC Forums

Ecere Developers Community

Numeric check

General help with the eC language.

Numeric check

Postby shakeshuck » Wed Dec 16, 2015 9:21 am

Hi again,

Is there an isNumeric for strings or something similar? FloatFromString needs some input vetting before it's used.

I can do it by hand, but just thought I'd ask as I would've thought a lot of folk would use it?
If I'm the first then of course I could be wrong... :lol:
shakeshuck
 
Posts: 42
Joined: Tue Dec 01, 2015 6:52 pm

Re: Numeric check

Postby jerome » Wed Dec 16, 2015 9:57 am

Hi shakeshuck,

There are some options here.
eC being a C super-set, anything C will work (e.g. strtod(), strtol()).

The current eC way is probably calling OnGetDataFromString() on your numeric type:

Code: Select all
float x;
if(a.OnGetDataFromString(string))
   PrintLn("Successfully parsed float value: ", x);


This will however return success even if there are further characters after a numeric value.
Use strtol() or strtod() for finer control, which returns a pointer saying where the parsing ended.

You may also want to look into using a SavingDataBox with a data type set to float or double, which will automatically convert to numbers and reject invalid input.

Regards,

-Jerome
jerome
Site Admin
 
Posts: 622
Joined: Sat Jan 16, 2010 11:16 pm

Re: Numeric check

Postby shakeshuck » Wed Dec 16, 2015 11:04 am

jerome wrote:You may also want to look into using a SavingDataBox with a data type set to float or double, which will automatically convert to numbers and reject invalid input.


The input fields are ListBoxes; I did initially try setting the data type, but then discovered alignment doesn't work if you do. :(
(With reference to the other thread on number alignment, in this case I wanted the value centered).

Use strtol() or strtod() for finer control, which returns a pointer saying where the parsing ended.


I'll take a look at these, thanks.
shakeshuck
 
Posts: 42
Joined: Tue Dec 01, 2015 6:52 pm

Re: Numeric check

Postby jerome » Wed Dec 16, 2015 1:34 pm

If you set the data type to e.g. float for a ListBox field, the data will normally be displayed with the alignment you setup for that field. But only when editing that data, it will be left-aligned.

This could be a work around for now, if you want the editing to be e.g. right-aligned as well.
Code: Select all
class MyFloat : float
{
   Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
   {
      EditBox editor = (EditBox)class::OnEdit(dataBox, obsolete, x, y, w, h, userData);
      editor.anchor = { top = 2, right = -4 };
      editor.autoSize = true;
      editor.OnPostCreate();
      return editor;
   }
}


However, this requires a tweak in DataBox.ec, otherwise the DataBox will currently unset that autoSize property if autoSize is not set on the DataBox itself (and auto-size DataBox don't work well with the ListBox):
Code: Select all
if(autoSize) // Need to add this if check
   ((EditBox)editor).autoSize = autoSize;


This is the line in the source:
https://github.com/ecere/ecere-sdk/blob ... ox.ec#L132

Then this should work:
Code: Select all
DataField fld3 { dataType = class(MyFloat), alignment = right, width = 120, editable = true };
jerome
Site Admin
 
Posts: 622
Joined: Sat Jan 16, 2010 11:16 pm

Re: Numeric check

Postby shakeshuck » Wed Dec 16, 2015 4:16 pm

jerome wrote:If you set the data type to e.g. float for a ListBox field, the data will normally be displayed with the alignment you setup for that field. But only when editing that data, it will be left-aligned.


That's what I thought, but it isn't doing that for me; with alignment set to center, centering (at display time) is not occurring if I change the dataType to int or float.
Of course it's highly possible I've unset something somewhere else that's causing this to happen... :roll:
shakeshuck
 
Posts: 42
Joined: Tue Dec 01, 2015 6:52 pm

Re: Numeric check

Postby jerome » Thu Dec 17, 2015 12:03 am

Please post some sample code to compare.
If you follow my example above it should work.
(But you will need the custom type and the libecere tweak to get the 'edited' data box to follow the alignment as well)
jerome
Site Admin
 
Posts: 622
Joined: Sat Jan 16, 2010 11:16 pm

Re: Numeric check

Postby shakeshuck » Thu Dec 17, 2015 5:10 am

Video sent, using same MakeActive sample as before...

Cheers. :)
shakeshuck
 
Posts: 42
Joined: Tue Dec 01, 2015 6:52 pm

Re: Numeric check

Postby jerome » Fri Dec 18, 2015 1:41 am

Hi shakeshuck,

I see what you mean.
What happens is that the data types have a 'default alignment', and setting the dataType property after the alignment will set it to that default alignment. If you change the code to set the alignment after the dataType it will work again.

Regards,

Jerome
jerome
Site Admin
 
Posts: 622
Joined: Sat Jan 16, 2010 11:16 pm


Return to eC Language

Who is online

Users browsing this forum: No registered users and 1 guest

cron