Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a web form that has a textbox that is formatted using a string code.

C#
TextBoxTHUG.Text = string.Format("{0:0,0}", double.Parse(TextBoxTHUG.Text));


This code turns 1234567 into 1,234,567. I also have a textbox text change code to do calculations for this textbox.

C#
protected void TextBoxTHUG_TextChanged(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBoxTHUG.Text);
        int j = 12;
        TextBoxTHUGDR.Text = Convert.ToString(i / j);
        TextBoxTHG.Focus();
    }


I am trying to get it to calculate put get this error "Input string was not in a correct format". Is there a way to keep the format and calculate or do I have to remove the format and then calculate?
Posted

You can't calculate strings. You need to convert it to numbers.
Try Int32.TryParse[^] method.


Example:
C#
String sNumber = "123,456,789";
int iDenominator = 12;
NumberStyles numStyle = NumberStyles.AllowThousands ;
CultureInfo culture = new CultureInfo("en-US");
int retValue = 0;

bool resultOfConversion = Int32.TryParse(sNumber, numStyle, culture, out retValue);

if (resultOfConversion == true)
    retValue = (int)retValue / iDenominator;


Console.WriteLine("Result: {0}", retValue );
Console.ReadKey();


Note: works perfect ;)
 
Share this answer
 
v3
Comments
Computer Wiz99 7-Oct-14 11:20am    
Maciej Los, thanks for the link. Can you show me how this would work in my code?
Maciej Los 7-Oct-14 11:25am    
You mean, you want to write code for you. I can do that, but you'll never learn. Try to solve it yourself, OK?
Computer Wiz99 7-Oct-14 11:30am    
I just wanted to see where the Int32TryParse will go. Plus, is there a way for the format to be entered after the calculations?
Maciej Los 7-Oct-14 11:31am    
Follow the link, there is an example.
Yes, it is. You need to convert it to string again ;)
Maciej Los 7-Oct-14 14:28pm    
I wan't detailed as i had to, sorry. Please, see updated answer.
Easiest solution is to use String.Replace Method[^].
 
Share this answer
 
v2
Comments
George Jonsson 7-Oct-14 14:04pm    
Trim only works at the beginning or end, right?
There are different methods. Trim() will do from start and end. TrimStart() will do it from start and TrimEnd() does it from end.
Matt T Heffron 7-Oct-14 14:12pm    
Nope. Trim() will remove from either end, TrimStart() from the start, and TrimEnd() from the end.
Remove() or Replace() are the methods to remove from "inside"
Yes, Matt. My bad. Bed time issues !!!
Updated my answer. Thanks. :)
Hi,

Check for value is not blank.

if(TextBoxTHUG.Text!="")
{
....
}

Regards,
Jatinath Jadhav
 
Share this answer
 
Comments
Maciej Los 7-Oct-14 11:22am    
The problem is that the numbers are formated with custom format. The only way to solve it is to use TryParse method. Please, see my answer.
BTW: i voted 2...
You can use
C#
int.Parse(TextBoxTHUG.Text.Replace(",", ""));
 
Share this answer
 
Comments
Computer Wiz99 8-Oct-14 9:33am    
George Jonsson, where will I apply this code? In Textbox_text change?

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