472,986 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

How format textbox so that "2" becomes "2.00"?

Stupid q for the day, but if the user enters 2 in a textbox, I want to
display as 2.00.

If they enter 2.129 I want it as 2.13.

I'm guessing will code in the validated method of the textbox, but not sure
how to convert the number.

Thanks for the help.

Jun 1 '08 #1
3 12155
M#
On Jun 1, 4:27 pm, "Ronald S. Cook" <rc...@westinis.comwrote:
Stupid q for the day, but if the user enters 2 in a textbox, I want to
display as 2.00.

If they enter 2.129 I want it as 2.13.

I'm guessing will code in the validated method of the textbox, but not sure
how to convert the number.

Thanks for the help.
Hi Ronald,

Would something like the following work for you?

protected void Button1_Click(object sender, EventArgs e)
{
double d;

if(double.TryParse(TextBox1.Text, out d))
{
d = Math.Round(d, 2);
Label1.Text = string.Format("{0:.00}", d);
}
}

Also I've posted a link below that has helped me alot with string
formatting

http://blog.stevex.net/index.php/str...ing-in-csharp/

Happy coding!
Jun 2 '08 #2
There are couple of ways doing it...

Use Key_Press event as below on the textbox
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(! (char.IsDigit(e.KeyChar) || e.KeyChar == '.') ) //
use the keys that you want to handle
{
e.Handled = true;
}
}

and also textbox validation event as below

private void textBox1_Validated(object sender, EventArgs e)
{
double d;
if(double.TryParse(textBox1.Text,out d))
{
d = Math.Round(d,2);
textBox1.Text = d.ToString();
}
}

This should solve the problem.

However I would like to derive a class from Textbox and handle the
above events in the derived class and use the same on the form. This
will give you flexibility in the future.

-Cnu

On Jun 2, 1:27*am, "Ronald S. Cook" <rc...@westinis.comwrote:
Stupid q for the day, but if the user enters 2 in a textbox, I want to
display as 2.00.

If they enter 2.129 I want it as 2.13.

I'm guessing will code in the validated method of the textbox, but not sure
how to convert the number.

Thanks for the help.
Jun 2 '08 #3
On Jun 2, 8:44 am, Duggi <DuggiSrinivasa...@gmail.comwrote:
if(! (char.IsDigit(e.KeyChar) || e.KeyChar == '.') ) //
But what's with country where number separator is "," not "." like
Poland?
I don't think it is the best resolution.

Regrads
Jun 2 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Muscha | last post by:
Hello, I have a date string in the following format: "2003-10-29T17:44+00:00" When I put it to DateTime.Parse() method it throws an exception, how do I parse this date format? Thanks, /m
2
by: kiran | last post by:
Hi, I am able to create instance of Culture Info for different cultures. How will I get number format as "$ ###,###,##0.00" from CultureInfo.NumberFormat. I need it in this format so that I...
5
by: ABC | last post by:
How to initial the datetime variable as value to "01/01/2005 00:00:00"?
7
by: Stimp | last post by:
I want to output a number, say 20, as 20.00 i.e. I want to always have 2 decimal places I don't want any currency information, therefore String.Format("{0:c}") doesn't work for me. Can't...
5
by: Bob | last post by:
Hello Folks, I am bring backa data reader dr from the database/ So I have a number of fields such as dr To put this in a table I use "<td>" + dr.ToString() + "</td>" But I want to...
3
dnb
by: dnb | last post by:
Hi... Thanx for reply.... I don't want concat string... But I want to disply "15,000.00" instead of "15000" in MSHflexgrid. At this time in my MSHflexgrid "15000" format is displayed. ...
0
by: Ronald S. Cook | last post by:
Stupid question for the day but weirdly I don't have this yet. How can I format a textbox such that if the user enters a number like 4 it will become (on validated) 4.00. If they enter 5.1 it...
3
by: Ronald S. Cook | last post by:
Stupid q for the day, but if the user enters 2 in a textbox, I want to display as 2.00. If they enter 2.129 I want it as 2.13. I'm guessing will code in the validated method of the textbox,...
0
by: tvnaidu | last post by:
database file gets created using below sql file, then lua script calls "db.import config_ascc", then I get errors says "insert table failed for eventLog", below are my sql file entry, config file,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.