473,224 Members | 1,291 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,224 software developers and data experts.

Re: Formatting numeric data in textbox

I need to format any decimal value so that it is right aligned, thousands
are separated and shows 2 decimal places always.
It is good to show the info in the format the user prefers.
That is language/locale speciffic (CultureInfo in the .NET lingo)
So you should use something like ToString("N")
If you are sure you want 2 digits after decimal separator, then use "N2"

--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #1
8 6493
Mihai,
It is good to show the info in the format the user prefers.
That is language/locale speciffic (CultureInfo in the .NET lingo)
So you should use something like ToString("N")
If you are sure you want 2 digits after decimal separator, then use "N2"
Thank you.

I need to format numbers when form is initially shown and after user leaves
changed TextBox.
Which TextBox event should be used for this ? Where ToString("N2") call
should be placed ?

Andrus.

Jun 27 '08 #2
I need to format numbers when form is initially shown and after user leaves
changed TextBox.
Which TextBox event should be used for this ? Where ToString("N2") call
should be placed ?
I personally try to stay away from Mask.
I initialize the fields when I create the form (using ToString),
and I validate them when user tries to submit the data
(using Parse or TryParse)
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #3
I personally try to stay away from Mask.
I initialize the fields when I create the form (using ToString),
and I validate them when user tries to submit the data
(using Parse or TryParse)
I do'nt understant this.
In the sample code I posted Textbox is data bound to decimal property Amount
which has value 12
Winforms initializes this TextBox automatically.

How to force Textbox to show 12.00 when form is shown and TextBox does not
receive focus ?

DataGridView column has DefaultCellStyle.Format property for this.
I havent found .Format property when TextBox is not in DataGridView.

Andrus.

Jun 27 '08 #4
In the sample code I posted Textbox is data bound to decimal property
Amount which has value 12
Winforms initializes this TextBox automatically.
I am not sure if Winforms uses the CultureInfo for that (to use the proper
decimal/thousand separators).
If it does, then just let Winforms do the work.

Otherwise, you might have to capture the loss of focus and reformat the way
you want.

Personally (as user), I don't care much about stuff being reformated.
If I type .2 and remains like this, I'm ok with it.
But I do care a lot about separators (123,456 means a completely different
thing in Germany vs US)

--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #5
>In the sample code I posted Textbox is data bound to decimal property
>Amount which has value 12
Winforms initializes this TextBox automatically.

I am not sure if Winforms uses the CultureInfo for that (to use the proper
decimal/thousand separators).
If it does, then just let Winforms do the work.
The issue is not related to cultureinfo.
Isse is that it is not possible to show 12 with 2 decimal places when form
is opened.
Otherwise, you might have to capture the loss of focus and reformat the
way
you want.

Personally (as user), I don't care much about stuff being reformated.
If I type .2 and remains like this, I'm ok with it.
But I do care a lot about separators (123,456 means a completely different
thing in Germany vs US)
My issue is that my program calculates product cost etc. using 5 digits
after comma and this must remain.
Users want to see this value with 2 digits after comma and also to enter
this value.
DataGridView DefaultCellStyle has Format property which allows this but
there is no such property when TextBox is placed to form.

Andrus.

Jun 27 '08 #6
The issue is not related to cultureinfo.
Not quite. CultureInfo gives you the right decimal separator
(which is a good thing).
Isse is that it is not possible to show 12 with 2 decimal places when form
is opened.
ToString("N2")
DataGridView DefaultCellStyle has Format property which allows this but
there is no such property when TextBox is placed to form.
Than you have to do it yourself by calling ToString when the control looses
focus.
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #7
Mihai,
>DataGridView DefaultCellStyle has Format property which allows this but
there is no such property when TextBox is placed to form.
Than you have to do it yourself by calling ToString when the control
looses
focus.
When form is initially shown, control does not lose focus.
So there is no place to call ToString().

Andrus.

Jun 27 '08 #8
When form is initially shown, control does not lose focus.
So there is no place to call ToString().
Then you might have to do the same at creation,
not only at lose focus.
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #9

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

Similar topics

1
by: mmuriegas | last post by:
Numeric data is imported as integer's. Loss of the fractions throws the calculations off. I am following the trail of a previous programmer and found that he was impoting a file "import delimited",...
1
by: Mike P | last post by:
I am using a stored procedure to get a value from a table, but the parameter I need to pass is a numeric data type in the table. The SQL Parameter object in .NET seems to support most of the SQL...
2
by: sp | last post by:
Hello Everybody, How can I restrict the user entering non-numeric data in textbox control on server side using asp.net. Any Code sample or suggestions are appreciated. Thanks in advance sp
2
by: John A Grandy | last post by:
how do VB.NET numeric data-types match-up to SQL-Server numeric data-types ? is it safe to use declare VB.NET variables of type Integer to contain SQL Server columns of type Integer ?
3
by: loisk | last post by:
Hi, Can anyone tell me.... What is the equivalent of "" (blank of string data type) with numeric data type? The default of my numeric data type control is 0, and I'd like trigger the event...
1
by: Erik Nodland | last post by:
Hi Everyone, Just after some ideas and suggestions on how to do this. I have a large amount of numeric data which I would like to group given a distance as a parameter. IE. If my dataset was...
1
by: Jarosław Kozik | last post by:
How to formatting numeric value like 20,5 to money value like 20,50 USD using MS SQL server 2000 regards IJKK
8
by: Frank Swarbrick | last post by:
My DBA says that a column defined, for instance, as DECIMAL(11,2) and containing a value of 1.00 takes up no more space on the database disk than a column defined as DECIMAL(7,2) and containing a...
4
code green
by: code green | last post by:
I have a text field called notes in which users have typed almost anything they like. I need to find numeric data randomly inserted in there. So for example there may be a telephone number and an...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.