Click here to Skip to main content
15,867,929 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Convert value from textbox 1.000
This is value one (1) with three decimals

Need to convert to number without decimals

What I have tried:

Need to convert to number without decimals
Posted
Updated 29-Oct-20 0:43am
v2
Comments
Maciej Los 28-Oct-20 16:45pm    
???
What have you tried till now?
Goran Bibic 28-Oct-20 16:48pm    
I have value in textbox 1.000 need to have on print value 1
Maciej Los 29-Oct-20 2:57am    
The question is NOT "what you've got?", but "what have you tried"?
Richard MacCutchan 28-Oct-20 16:55pm    
Your question is far from clear. Please explain exactly what you are trying to do.
Goran Bibic 28-Oct-20 16:59pm    
 pdfDoc.Add(new ListItem("   Quantity: " + ArtikliKolicinaTextBox.Text), calibri10bold));


Result is 1.0000

Need to be result 1

First, convert your textbox entry to a number using TryParse:
C#
double result;
if (!double.TryParse(ArtikliKolicinaTextBox.Text, out result))
   {
   ... report problem to the user - bad number entry ...
   return;
   }

Then you can output it without any decimals either with a Round method, or (simpler) by casting it to a integer:
C#
pdfDoc.Add(new ListItem($"   Quantity: {(int) result}"), calibri10bold));
 
Share this answer
 
You only want digits 0~9: don't let the user enter anything else:// assume you want to allow the minus sign and backspace:
C#
private string legalChars = "-\b";

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    char c = e.KeyChar;

    e.Handled = !
    (
        char.IsDigit(c)
        || legalChars.Contains(c)
    );
}
Left for you to code: allow only one minus sign to be entered as the first character Advanced: handle cut and paste.
 
Share this answer
 
v4
I'd suggest to read this:
Standard numeric format strings | Microsoft Docs[^]
Custom numeric format strings | Microsoft Docs[^]

In other words, if you would like to print number in specific format, you need to use string formatting. For example:
C#
double d = 123.123;
CultureInfo ci = new CultureInfo("en-us");
Console.WriteLine(d.ToString("F0", ci));
//result: 123
 
Share this answer
 
You can do it by two way

1) By using math.round

n = 7.245;
a = System.Math.Round (n, 2, MidpointRounding.ToEven);       // 7.24


you can change 2 with zero because you dont want anything after decimal..


1) By using Convert.ToInt32

decimal value = 7.24m;
int n = Convert.ToInt32(value);                            // 7
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900