0

The value that extracted from the application is in string format for ex. "$0.38". So, I segregated each character in the given string using IsDigit then appended them together using string builder. The digit can also be alphanumeric like "12,365.23 AS". Is there a way to recover only numeric part (along with the decimal) from the given string. But Output I receive is "38" instead of "0.38". I also want to compare that the given string value lies between the upperLimit and lowerLimit provided. Please let me know how to proceed with the same.

string Value = "$0.38";

int upperLimit = 2500;
int lowerLimit = 50000;

StringBuilder sb = new StringBuilder();
//sb.Append(someString);
foreach (char amin in Value)
{
    if (System.Char.IsDigit(amin))
    {
        sb.Append(amin);
    }
}
int compareVal = Convert.ToInt32(sb.ToString());

Console.WriteLine("value for comparision" + " " + compareVal);
Ansh
  • 57
  • 1
  • 1
  • 9

3 Answers3

2

There are two reasons why you will get 38:

  1. StringBuilder looks like "038", since "." is not a digit (just like "$").
  2. Convert.ToInt32(...) returns an integer which doesn't allow decimal digits.

The better data type for currencies is decimal, a high precision floating point data type so to say.

Try

var amount = decimal.Parse(Value , NumberStyles.Currency)
var isInLimit = upperLimit <= amount && amount <= lowerLimit; // i guess you swapped upper and lower limit btw. ;)

instead.

Edit

In order to use the NumberStyles-Enumeration, you will have to use tha correct namespace in your file:

using System.Globalization;
nozzleman
  • 9,529
  • 4
  • 37
  • 58
2

The best way is using one of the overloads of decimal.Parse:

string Value = "$0.38";
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");

decimal dd=decimal.Parse(Value, System.Globalization.NumberStyles.AllowCurrencySymbol|System.Globalization.NumberStyles.AllowDecimalPoint,culture);

Note the use of NumberStyles enum.That way you can control exaclty the parsing.

Pikoh
  • 7,582
  • 28
  • 53
  • Please don't parse currencies as `double`, use `decimal` in order to support penny-values like `0.1`. – grek40 Dec 21 '16 at 10:31
1

You are omitting the decimal point and you are not using a decimal data type to hold the converted value. The real way to go is to convert the currency string to a decimal number:

CultureInfo usCulture = new CultureInfo("en-US)";
decimal amount = decimal.Parse(Value, NumberStyles.Currency, usCulture);

You can then perform a proper numeric comparison:

if (amount <= upperLimit && amount >= lowerLimit)
    ....

I first marked the question as a duplicate, but then changed my mind. I still think it is very much related to: Convert any currency string to double

Community
  • 1
  • 1
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • I am getting "The name 'NumberStyles' does not exist in the current context" this error when I try to use this – Ansh Dec 21 '16 at 10:35
  • @Ansh You need to add the `System.Globalization` namespace, of course... – Thorsten Dittmar Dec 21 '16 at 10:36
  • let say if the string is in form of combination of alphabets and numerals, then how to proceed? I used string builder because of that. – Ansh Dec 21 '16 at 11:03
  • let say if the string is in form of combination of alphabets and numerals, then how to proceed? I used string builder because of that. Lets say it is "abcd1,002.38". Can you help me through in this case? – Ansh Dec 21 '16 at 11:04
  • Well, if the format of the string does not follow a given currency format, things get a bit harder. In that case it depends a bit on the number format, but I guess I'd limit the number of characters possible, for example to numbers, +, -, comma and period and use your string builder approach and the number conversion afterwords, or I'd look for a regular expression that extracts decimal numbers from strings and use that before the number conversion and comparison. – Thorsten Dittmar Dec 21 '16 at 11:26