Displaying a float in a number text box

There are many questions regarding converting a string to a textbox to a floating point value, or allowing a textbox type = "number" to allow decimal points, however I can't find anything to do with mapping a float to a textbox with type set to " number ".

So far, I have a textbox with type = "number" and step = "0.01". It also has a minimum value of "0". It shows the following:

<asp:TextBox ID="TextBox1" runat="server" TextMode="Number" step="0.01" Width="140px" min="0"></asp:TextBox>

      

It works fine for user input and saves data to varable / database without issue. However, if I want to display this same value to the user, it doesn't display anything, not even "0".

So my question is, how can I take the form of a "float" value, convert it to a "string" and apply it to the Textbox.Text so that it displays the value?

As an example: if I set the value in the textbox to be "5" it will save and display "5", but if I put "5.5" or "5.5" anyway it never displays it and I need to display "5.5".

Any ideas?

PS: Supposed to represent monetary value. I would also prefer to have a solution that doesn't require Javascript if possible.

+3


source to share


5 answers


Float 5.5 is not exactly the real value 5.5 (see .NET Float Reference )

I highly recommend that you NEVER use floats for monetary values! It's best to use decimal type for these things.

That said, if you really want to use float when you read a value from the DB, do this:

TextBox1.Text = ((decimal)myNum.ToString( "0.##" )).Tostring()

      



With this solution you can leave the step as you are "0.01"

PS As Thiago pointed out, there might be a mismatch between your culture and the floating point separator. decimals expect periods, but your number may have a comma. In this case, do this:

decimal.Parse("18,285", new NumberFormatInfo() { NumberDecimalSeparator = "," })

      

got it from this question

+1


source


set the any

value in step .

 <input type="number" step="any"/>

      

You can use Convert.ToSingle()

or float.Parse

to convert string to float



var value = Convert.ToSingle(TextBox1.Text, CultureInfo.InvariantCulture);
 //Or
 var value1 = float.Parse(TextBox1.Text, CultureInfo.InvariantCulture);

      

And use ToString("N")

instance method to convert float to string.

var str = floatNum.ToString("N"); // or N2 (2 digit decimal)

      

+1


source


Ok, after doing some research on the type of textbox number, I found that HTML5 input type = "number" is a mess. I know this is not very interesting for some, but I know of one or two programmers who will be facing the same problem.

@KarmaEDV has a better answer for the problem as decimal values ​​are the correct way to handle currency. However, @AVD is also right where if you set it to "any" it will accept any size value in decimal cases.

However, this leads to another problem. When you set an input with a type number, this field will only accept numeric values ​​or DOT (period). If you are not working with English notation, you can use "," (comma) as the decimal point. Crazy things happen here when the field takes a "," and when the value is stored it is still a valid decimal value, but when you convert the value (with ",") it will create a character string with a "" character. Example: The value 5.5 on the line "5.5" is literal.

As a box that does not accept ",", it will not display it. Decision?
  .ToString () Replace. ('' '.'); just as simple.

Want more? It still displays "5.5" s "," (comma) ... There's a reason why this is causing problems ...

Thanks everyone for your help.

+1


source


You can manually check the code using something like:

TextBox1.Text.ToString(CultureInfo.InvariantCulture);

      

You can also use

float.TryParse( TextBox1.Text) 

      

To see if it's a float value, or if you can treat it like a regular string.

0


source


A likely best practice is to forget text = "number" (due to the issues above) and validate user input with Decimal.TryParse (). For American currency, I also suggest making sure there are no more than two digits after the decimal point and (better) to make sure there are EXACTLY two digits to the right of the decimal point to catch possible input errors (if only one digit before the right of the decimal point can be unintentional).

0


source







All Articles