Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a calculation I am trying to do with data type float. I have never done one before using float so i am new to this. This is the code I have so far:

C#
float c = float.Parse(TextBox1.Text.Replace(",", "").Replace(".", ""));
float d = float.Parse(TextBox2.Text.Replace(",", "").Replace(".", ""));
TextBox3.Text = (c-d).ToString();


It does the calculations for the result I get is wrong. It has numbers and letters in the textbox3.

This is what I am trying to calculate for each textbox. I am doing a subtraction.

C#
float c = float.Parse(TextBox1.Text.Replace(",", "").Replace(".", "")); //$46,534,798.18

float d = float.Parse(TextBox2.Text.Replace(",", "").Replace(".", "")); //$44,441,222.00

TextBox3.Text = (c-d).ToString(); //4.65348E+11


Please help me!!

What I have tried:

C#
float c = float.Parse(TextBox1.Text.Replace(",", "").Replace(".", ""));
float d = float.Parse(TextBox2.Text.Replace(",", "").Replace(".", ""));
TextBox3.Text = (c-d).ToString();
Posted
Updated 23-Jan-20 6:08am
v2
Comments
Member 13566383 23-Jan-20 4:54am    
Show us the content of your TextBox contents, the values of your variables c and d and the result contained in TextBox3.Text.
"The result is wrong" is not a meaningful error description.
Computer Wiz99 23-Jan-20 9:45am    
Sorry about the long wait and the missing data. This is a subtraction calculation.

float c = float.Parse(TextBox1.Text.Replace(",", "").Replace(".", "")); //$46,534,798.18

float d = float.Parse(TextBox2.Text.Replace(",", "").Replace(".", "")); //$44,441,222.00

TextBox3.Text = (c-d).ToString(); //4.65348E+11
Richard MacCutchan 23-Jan-20 10:15am    
Your text values include the $ sign which will cause the Parse call to fail.
Computer Wiz99 23-Jan-20 10:23am    
I replace them and still get the same issue.
float c = float.Parse(TextBoxTBS1.Text.Replace(",", "").Replace(".", "").Replace("$", ""));
float d = float.Parse(TextBoxTBS2.Text.Replace(",", "").Replace(".", "").Replace("$", ""));
Richard MacCutchan 23-Jan-20 10:38am    
Stop doing all these Relace calls, they are unnecessary. Use a Substring call to skip the $ sign:
float.Parse(TextBoxTBS1.Substring(1));

Better still use TryParse, as already suggested by phil.o below.
if (!float.TryParse(TextBoxTBS1.Substring(1), out c))
{
   // Handle here the case where a float value could not be properly parsed
}

You do not need/should not trim decimal and group separators from the strings you are trying to parse.
You should instead use Single.TryParse Method (System) | Microsoft Docs[^] , especially the overload which lets your specify the culture:
C#
float c, d;
NumberStyles styles = NumberStyles.Float | NumberStyles.AllowThousands;
if (!float.TryParse(TextBox1.Text, styles, CultureInfo.CurrentCulture, out c))
{
   // Handle here the case where a float value could not be properly parsed
}
if (!float.TryParse(TextBox2.Text, styles, CultureInfo.CurrentCulture, out d))
{
   // Handle here the case where a float value could not be properly parsed
}

If you remove decimal separator, there is a great chance that the value you will get will be incorrect (120.23 would become 12023, for example).
 
Share this answer
 
v2
Comments
[no name] 23-Jan-20 13:15pm    
What does it help to use CultureInfo.CurrentCulture from the server?
phil.o 23-Jan-20 13:23pm    
Probably not much; but then the whole thing would be kind of useless since the content of the textboxes would need to be known on server side for it to work.
This is what I did and it worked.

C#
decimal c;
c = 0;
c = decimal.Parse(SELTOTAL1.GetValue(0).ToString());

decimal d;
d = 0;
d = decimal.Parse(SELTOTAL2.GetValue(0).ToString());

decimal x = c - d;
TextBoxTBS3.Text = x.ToString();
 
Share this answer
 
Comments
Richard MacCutchan 23-Jan-20 12:17pm    
1. Don't use Parse, use TryParse.
2. Why are you converting a value to a string just to convert it back to a value?

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