10

There is a lot of questions regarding the conversion of a Textbox string to a float value, or allowing a Textbox of type="number" to allow decimal points, however, I can't seem to find anything related to displaying a float value to a Textbox with it's type set to "number".

So far, what I have is a Textbox with type="number" and step="0.01". It also has a min value of "0". It shows like this:

<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 the data to a varable/database without any problem. However, if I want to display this same value back to the user, it doesn't display anything, not even a "0".

So my question is: How can I take the value form a "float", 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 as "5", it will save and display "5", but if I put "5.5" or "5,5", either way, it never displays it, and I need it to display "5.5".

Any ideas?

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

Tiago Cachinho
  • 323
  • 1
  • 4
  • 15
  • Your question answered a question I had about how to make the input accept the decimal number, and not force me into using an integer. Thanks! – NovaDev Mar 25 '21 at 17:27

5 Answers5

4

set any value to step attribute.

 <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") of instance method to convert float to string.

var str = floatNum.ToString("N"); // or N2 (2 digit decimal)
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
3

A float 5.5 is not exactly a real value of 5.5 (see .Net Float Reference)

I strongly suggest you to NEVER use floats for monetary values! It is best practice to use a decimal type for These things.

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

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

With this solution you can leave the stepping as you whish "0.01"

P.S. As Tiago pointed out there can be a mismatch between your culture and the floating point separator. decimals expect dots but maybe your number has a comma. In this case convert like that:

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

got it from this question

Community
  • 1
  • 1
KarmaEDV
  • 1,631
  • 16
  • 27
  • Ah, I see! Decimal would be the missing piece here. Is decimal translated to "Real" in SQL? – Tiago Cachinho Jun 30 '15 at 11:48
  • 1
    @TiagoCachinho no, SQL knows decimal as well. [SQL DECIMAL()](https://msdn.microsoft.com/de-de/library/ms187746(v=sql.120).aspx) – KarmaEDV Jun 30 '15 at 11:52
  • Ok, so I tested it, and it looks like it saves "better" in SQL, and the value is well used throughout the program, but it still isn't rendered in the textbox... Am I missing something? BTW, I tried with ToString("0.##") and straight up ToString() but it's the same. I also tried ToString("N2"). – Tiago Cachinho Jun 30 '15 at 12:23
  • Another thing I tested, just in case, is to set the textbox to type="text", to test if the value is correctly displayed. In fact, the value is correct and it shows on the textbox, but it's still a type="text" and I need a type="number". – Tiago Cachinho Jun 30 '15 at 12:38
2

Well, after some research on Textbox type number, I discovered that HTML5 input type="number" is a mess. I know it is not really interesting for some, but I know one or two programmers that will bump into the same problem.

@KarmaEDV has the best answer for the problem, as decimal type values are the correct way to process currency. However, @AVD was also right, where if you set it to "any" it will accept values of any size in decimal cases.

Meanwhile, it lead to another problem. When you set an input with the type number, that box will only receive values that are numerical or DOT (point). If you're not working with english notations, you might use "," (comma) as your decimal point. Crazy things happen here, where the box accepts "," and when the value is saves, it is still a valid decimal value, but when you convert the value (with the ","), it will create a string of character with ",". Ex.: the value 5,5 is in string "5,5" literally.

As the box those not accept ",", it won't render it. Solution?
.ToString().Replace(',','.'); as simple as that.

Want more? It still displays "5,5" with the "," (comma)... There's the reason why it causes trouble...

Thank you all for your help.

Tiago Cachinho
  • 323
  • 1
  • 4
  • 15
0

You can manually check in the code-behind using something like :

TextBox1.Text.ToString(CultureInfo.InvariantCulture);

You can also use

float.TryParse( TextBox1.Text) 

To see if the value is a float, or if you can treat it as a normal string.

Johan
  • 262
  • 1
  • 15
0

Probable best practice is to forget Text="Number" (due to problems specified above) and validate user input using Decimal.TryParse(). For US currency, I also suggest making sure there are no more than two digits after the decimal point and (better) to make sure that there are EXACTLY two digits to the right of decimal point to catch possible entry errors (if only 1 digit to right of decimal point could be unintended).

Bruce Allen
  • 191
  • 2
  • 7