Dark Knight
Dark Knight

Reputation: 3577

displaying numbers in correct format in textbox C#

the number entered in the textbox is stored as integer,suppose if you enter a number like 0095 on exiting textbox it should displayed "95",also another textbox takes float,if i enter 1.567 it should be displayed as"1.5"...how to do that in C#?

Upvotes: 0

Views: 819

Answers (1)

Jim
Jim

Reputation: 3284

Like most other languages I assume. In C# when I do something like that, I get the data from the textbox,usually richtextbox, store it as a string. from there you can typecast it to an int (should you believe that what is being typed is an int) and then do a fairly straight forward trim of the float number.

Use readline to take lines as strings, or just take the whole thing as a string, depending on how you implement it.

Convert strings to ints using:

int numVal = Int32.Parse("105");

or

string text = "500";
int num = int.Parse(text);

and then

Math.Round(float,decimals)

for trimming the float.

Upvotes: 2

Related Questions