String Conversion to Int

  • Thread starter Thread starter ComputerGuy
  • Start date Start date
C

ComputerGuy

This is a newbie question. Let me know if I'm in the wrong place.

I working with an external application that returns two strings that I
need to divide.
val1 = "$100,000"
val2 = "$45"

I know I can't divide the numbers because they are both strings. So I
removed the "$" and the ",". Then I explicitly converted them,
System.Convert.ToInt32(val).

However this causes an error. Is there something that I am missing?

Thanks for the help.
 
This is a newbie question. Let me know if I'm in the wrong place.

I working with an external application that returns two strings that I
need to divide.
val1 = "$100,000"
val2 = "$45"

I know I can't divide the numbers because they are both strings. So I
removed the "$" and the ",". Then I explicitly converted them,
System.Convert.ToInt32(val).

However this causes an error. Is there something that I am missing?

Thanks for the help.

Hmm... shouldn't give you a problem. A better idea instead of
manually stripping those characters would be int.Parse.
 
Hmm... shouldn't give you a problem. A better idea instead of
manually stripping those characters would be int.Parse.

I tried the int.Parse but it still doesn't output anything. Any other
suggestions?
 
Inefficient, but here you go:

int result = int.Parse (CleanValueString (val2)) / int.Parse
(CleanValueString (val1));

static string CleanValueString (string str)
{
return str.Replace ("$", "").Replace (",", "");
}

I would recommend working on making it more efficient (if you need to) and
checking for division by zero. You also might want to use a double instead
on an int etc.

Hilton
 
Inefficient, but here you go:

int result = int.Parse (CleanValueString (val2)) / int.Parse
(CleanValueString (val1));

static string CleanValueString (string str)
{
return str.Replace ("$", "").Replace (",", "");
}

I would recommend working on making it more efficient (if you need to) and
checking for division by zero. You also might want to use a double instead
on an int etc.

Since it seems that the numbers represent currency amounts, double is a
terrible, terrible choice to represent the values.
 
I have to give credit to Nicholas Paldino. He answered something similar
that I adapted to this question:

string val1, val2;
val1 = "$100,000";
val2 = "$45";
double quotient = 0.0 ;
double price1= 1, price2 = 2;

price1 = double.Parse(val1, System.Globalization.NumberStyles.Any);
price2 = double.Parse(val2, System.Globalization.NumberStyles.Any);

quotient = (price1 / price2);

Console.Out.WriteLine(string.Format("{0} / {1}", price1, price2));
Console.Out.WriteLine(string.Format("Quotient: {0}", quotient));

System.Threading.Thread.Sleep(2500);
 
ComputerGuy said:
This is a newbie question. Let me know if I'm in the wrong place.

I working with an external application that returns two strings that I
need to divide.
val1 = "$100,000"
val2 = "$45"

I know I can't divide the numbers because they are both strings. So I
removed the "$" and the ",". Then I explicitly converted them,
System.Convert.ToInt32(val).

However this causes an error. Is there something that I am missing?

My suggestion:

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.CurrencyDecimalSeparator = ".";
nfi.CurrencyGroupSeparator = ",";
nfi.CurrencySymbol = "$";
string val1 = "$100,000";
string val2 = "$45";
decimal d1 = decimal.Parse(val1, NumberStyles.Currency, nfi);
decimal d2 = decimal.Parse(val2, NumberStyles.Currency, nfi);

Arne
 
Family said:
I have to give credit to Nicholas Paldino. He answered something similar
that I adapted to this question:

string val1, val2;
val1 = "$100,000";
val2 = "$45";
double quotient = 0.0 ;
double price1= 1, price2 = 2;

Don't use double for monetary amounts.
price1 = double.Parse(val1, System.Globalization.NumberStyles.Any);
price2 = double.Parse(val2, System.Globalization.NumberStyles.Any);

Why did you initialize these variables to throwaway values?
 
Why did you repeat yourself?

I realise of course that you used different words, but it amounts to a
repetition.
 
Lew said:
Family Tree Mike wrote: ....

Don't use double for monetary amounts.

I'm not. I'm using them for the numerator and denominator in a mathematical
equation.


....
Why did you initialize these variables to throwaway values?

I inited the values to get around a compiler warning with a previous try and
left it in. The variables are initialized to zero then thrown away anyways...
 
Family said:
I'm not. I'm using them for the numerator and denominator in a mathematical
equation.

Well you code also included:

val1 = "$100,000";
val2 = "$45";

and:

price1 = double.Parse(val1, System.Globalization.NumberStyles.Any);
price2 = double.Parse(val2, System.Globalization.NumberStyles.Any);

so yes - you used double for monetary amounts.

Arne
 
Stephany said:
Why did you repeat yourself?

Look at the order of events:

1) X suggest double
2) Lew say don't
3) Y suggest double
4) Lew say don't

Apperently Y missed the first post by Lew.

Arne
 
Or, I had a difference in opinion. Currency divided by currency is not
currency. Look at an example. $100,000 in stock, divided by $45 price per
share, gives total shares, which can include partial shares. The result is
not currency.
 
Family said:
Or, I had a difference in opinion. Currency divided by currency is not
currency. Look at an example. $100,000 in stock, divided by $45 price per
share, gives total shares, which can include partial shares. The result is
not currency.

And ?

Nobody are complaining over that you divide two doubles.

The complains are over parsing two amounts into doubles (before
dividing).

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top