473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 12265
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.