In Field from react-final-form how to convert input value to float only on blur? - react-final-form

If I use 'parse', then the user can not enter the decimal separator.
Because "9." is parsed as 9 and instantly replaces the input text.
Sandbox: https://codesandbox.io/s/xmj92nnpo

Yep. This is a common problem. If you want to allow decimal points, you have to keep the value as a string, and then convert it to a number at submit time. You can still use parse to remove any illegal characters (or type="number" on your <input>), but you can't parse it into a number on every keypress or you will lose your decimal points.

I end up with my own StatefulInput component, with 'editing' flag in the internal state. And I call Field's 'onChange' event handler only on blur.
https://codesandbox.io/s/2zpn29zzjj

Related

Change Masked Text into Plaintext from DBEdit

I have a DBEdit control, connnected to an ADO record set displaying a floating point number. This number has been masked as a currency through the field editor of the record set. This causes a currency sign to be displayed at the front of the number, eg 1.5923432 -> £1.59. I'd like the user to be able to edit the number, and then click a button that pushes the edited number to the database. However, when I get the text content of the box (using strtofloat(DBEdit1.text)) the result produces an error as the text of the dbedit does of course include the currency sign, so cannot be converted to a number.
How can I get the plaintext contents of the DBEdit, (without the currency symbol) without any masking? Would simply deleting the first character from the front of the string be an effective way to do it or is there a simpler way?
I figured it out. You can get the contents of a dbedit with no masking by using: DBEdit1.Field.AsReal or DBEdit1.Field.AsInteger
This will give you the numerical value contained within the dbedit, with no currency signs or commas.

iOS - Best way to get numbers out of NSString! (Geo coordinates)

For my internship I'm working on a App that uses GPS data! That's already implemented and I wrote a class which converts the double-value the mapView sends into an user-picked format for Geo Coordinates (Degrees, Degrees-Minutes or Degrees-Minutes-Seconds)! Now there are also text fields the user should enter some coordinates in for adding waypoints!
What's the best technique here to get a the seperate numbers out of a string in a format similar to this 57° 14' 03" N!
Since it's a user input, the format won't be this, it's only similar! So is it better to parse these out the string or maybe limit the users input method from a textfield to something more strict which only allows one format (separate textfields for each number f.ex.)!
Actually a question to UX rather than a how-to-do!
Acting as the delegate of the text field and not allowing invalid content / format is a good first step.
For parsing the string, NSScanner is the appropriate class to use to split out the parts. If you tie the format down though, you could use componentsSeparatedByString: to separate each number by the space between them.
First, a comment. Ending all your sentences with exclamation points is silly!
As to your question. Yes, you should enforce a strict input format on your users. If you look in the software developers's dictionary, user is a synonym for idiot.
I would suggest separate UITextFields for each numeric value of each lat/long, with the symbols drawn in place with labels. The user would enter degrees, and the input would jump to minutes. The user enters minutes, and the input jumps to seconds. The user enters seconds, and the input jumps to degrees on the longitude.
Validate each input as a well-formed number.
If you want to use free-form input of strings like "57° 14' 03" N!", you might want to create a regular expression to validate it, plus range-checking on the numeric parts. If you Google it you should find online docs on regular expression. I don't use them often enough to be able to write a regular expression off the top of my head.

Why can't I enter a decimal point in a bound UITextView?

I'm binding a UITextView to a field of data type float, and it seems that the binding mechanism is being over-vigilant in validating my data such that I cannot actually enter floating point numbers.
Specifically, if I type "7" in my UITextView, the float field in the data model updates. If I paste "7.2" in the text view, it also updates. But if I type "7." (on the way to typing "7.2"), the binding fails with error:
MvxBind:Error:365.27 SetValue failed with exception - FormatException: Invalid format.
This is arguably correct, since "7." isn't a valid numeric string.
But it also discards the decimal point, and so I can't enter my number. It just leaves "7" in the text view. The weird thing is that this is ONLY affecting the decimal point/period character. If I type "7X", it doesn't bind, but it does let me type the X. What's going on here?
Just may be try to bind to string value and make a little manually in code string<=>float conversions? Also this should be much better way on monotouch specifically since based on the common .net functions rather than on the UI implementation.

How do I avoid errors when converting strings to numbers if I don't know whether I have floats or integers?

I have stringgrid on delphi form and i am trying to divide values of one cell with value of another cell in another column.
But the problem is, stringgrid cells are populated with different types of numbers, so I am getting ConvertErrors.
For example the numbers in cells can look like
0.37 or 34 or 0.0013 or 0.00 or 0.35 or 30.65 or 45.9108 or 0.0307 or 6854.93.
In another words I never know is it going to be real, float, integer or any other kind of type in those cells.
I have looked everywhere on internet but no luck. Anyone any ideas. By the way I am not exactly Delphi expert. Thanks.
For each string, convert it first to a float value using StrToFloat function in SysUtils.pas . This should allow for any numerical type to be dealt with (unless you have something unusual like complex numbers). As you have some zero values in your list above you should also ensure that you check for divide by zero conditions as this will also potentially throw an exception.
SysUtils has many functions such as TryStrToFloat, TryStrToInt, TryStrToInt64 etc for this purpose. These functions accept a reference parameter (var parameter) for returning the converted value and function itself returns true if the conversion is successful.
If you are sure that the string has a valid number then you can check the input string to see if it has a decimal point before deciding which function to use.
Treat all the numbers as float. Use StrToFloat, divide the numbers, and then convert the result back to string with FloatToStr. If the result is an integer, no decimal point would be produced.

How to disable scientific notation in .AsString in Delphi?

Hi
I want to get numbers from database, for example, if the number in database is 44.7890000000, I would like to get a string 44.789, the same 0.0010000000 -> 0.001, just keep the numbers and trim the tailing '0'.
I use this code:
qrySth.Fields[i].AsString - it does its job but I find for very small numbers like 0.0000010000 it becomes 1E-6. Is there a way I could disable the scientific notation for this AsString method?
Thanks!
As an alternative to setting the field's DisplayFormat property, you can read from AsFloat and pass the value directly to FormatFloat. It uses the same format pattern.

Resources