473,320 Members | 1,722 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,320 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 6504
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.