Jump to content
Sign in to follow this  
Gord P

TNumberBox and scientific notation (exponential format)

Recommended Posts

I typically use TEdits or StringGrids to let the user input numbers.  Sometimes for a given input, the number can either a regular float (123.456) or required to be entered with scientific notation (1.23456e11).  I thought maybe TNumberBox might be helpful with number input (i.e. automatically handle only entering valid numerics) so I checked it out but I am unable to get it to allow the user to enter an exponent.

 

Based on something online I think the the Microsoft's NumberBox control, which I assume TNumberBox is emulating or wrapping, has the ability do this.  Could be wrong here too though.

 

Is it not able to do this or is it just me?  I have fooled around with the DisplayFormat property but no joy.

 

 

Share this post


Link to post

maybe....

Quote

  NumberBox1.Mode          := TNumberBoxMode.nbmFloat;
  NumberBox1.Decimal       := 0; // ??
  NumberBox1.DisplayFormat := '0.0E+;0.0E-;Hello';

 

Project1_fcmc90Pbia.gif

Share this post


Link to post

Thanks for the suggestion.  Unfortunately it doesn't give me the desired behavior.

 

I would like the user to be able to enter a number such as 1.234E9.  The NumberBox will not allow typing in an 'E'.

Share this post


Link to post
3 hours ago, Gord P said:

The NumberBox will not allow typing in an 'E'.

Seems like a feature request in QP would be appropriate.

  • Like 1

Share this post


Link to post
7 hours ago, David Heffernan said:

What's wrong with a simple edit control? 

Not too much really and that is what I have been using - along with some code to check or prevent incorrect input.  I was just checking to see if the TNumberBox control would have that built in and maybe some additional features since the word "Number" is in the name of the control.

Share this post


Link to post

maybe some like this ???

  • NOTE: when replace a value on NumberBox (old values) was verifyed that "e" will be accepted!!! then needs more tests... ok?
  • NOTE2: NumberBox1.Text is always "old value", better works to "NumberBox1.VALUExxxxx"
type
  TForm1 = class(TForm)
    NumberBox1: TNumberBox;
    procedure FormCreate(Sender: TObject);
  private
    procedure MyOnValidChar(AChar: char; var AValidated: boolean);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FormatSettings.DecimalSeparator := '.';
  //
  NumberBox1.Mode           := TNumberBoxMode.nbmFloat;
  NumberBox1.Decimal        := 20; // ??
  NumberBox1.DisplayFormat  := '0.0E+;0.0E-;Hello';
  //
  NumberBox1.OnValidateChar := MyOnValidChar;  // <------
end;

procedure TForm1.MyOnValidChar(AChar: char; var AValidated: boolean);
begin
  if CharInSet(AChar, ['E', 'e']) then
    begin
      if NumberBox1.ValueFloat = 0 then // eNNNN.NNN ???
        AValidated := false
      else
        AValidated := string(NumberBox1.Text).ToLower.CountChar('e') = 0; // NOT "Neeee"
    end;
end;

 

 

Project1_4ZCOi6Lrb1.gif

Edited by programmerdelphi2k

Share this post


Link to post

Thanks for that.  In the end though if I have to add code, it's not much better than the Edit control for now.  It's not a big deal, I was just wondering if I was missing something on this control, but I guess not.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×