0

I have the following strings

string a = "1000" string b = "£1,000".

I am doing an Assert.Equal to try and compare the two strings but I need to format one of them to be exactly the same as the other.

I have looked at numerous StackOverflow posts and none have exactly what I require.

I have tried the following:

string getAmount = "1000"

string myStrong = String.Format("{0:#,###.##", getAmount)

but it doesn't work, I get "Input string was not in a correct format" Can anyone help?

Thanks

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
RShome
  • 489
  • 2
  • 11
  • 35
  • 1
    What are you actually trying to do? Are you trying to determine whether two `strings` contain representations of the same number? – John Sep 22 '22 at 16:25
  • yes correct. Sorry I wasn't more clear. – RShome Sep 22 '22 at 16:26
  • 3
    You can't format a `string`. You can only format something else, e.g. a number, as a `string`. To do what you describe - I'm not convinced that that's a good idea but we'll see - then you'd need to convert the `string` to a number first, then format that. – John Sep 22 '22 at 16:26
  • 3
    *"yes correct"*. In that case, you should convert both `strings` to numbers first, then compare the numbers. Read the documentation for the `Parse` and `TryParse` methods of the appropriate numeric type (probably `Int32` or `Decimal`) and see how you can allow for things like currency symbols. – John Sep 22 '22 at 16:28
  • Perfect, this is what worked for me //Convert string to double then add currency formatting. double amount = assetsData[4].Value.ToDouble(); var expectedAmount = "£" + string.Format("{0:#,###.00}", Convert.ToDecimal(amount)); – RShome Sep 22 '22 at 16:47
  • 1
    Though the examples are whole numbers, you should be using `decimal` — not `float` or `double` — throughout for financial calculations. – Lance U. Matthews Sep 22 '22 at 16:49
  • 3
    If you want to format a number as currency then use the "C" format specifier. I still think that that's a bad solution though. If you aim is to compare numeric values then you should compare numeric values, not `strings`. – John Sep 22 '22 at 16:51
  • 1
    Remember, too, that when you use `,` and `.` in a format string those [aren't literal characters](https://learn.microsoft.com/dotnet/standard/base-types/custom-numeric-format-strings) but represent group and decimal separators for the specified/current culture. Thus, for some cultures `string.Format("{0:#,###.00}", 12345.6789)` will return `12.345,68` with those separators seemingly transposed. You could "fix" that by escaping those characters, but better to do as @John suggests and just compare them as numeric values since those, without the human-friendly formatting, are what really matter. – Lance U. Matthews Sep 22 '22 at 17:08
  • 1
    Does this answer your question? [Compare 2 prices as datatype string in c#](https://stackoverflow.com/questions/40300527/compare-2-prices-as-datatype-string-in-c-sharp) That will work if `System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol` is `"£"`, otherwise you can pass, for example, `CultureInfo.GetCulture("en-GB")` as the third parameter. See also `decimal.TryParse()`. Also, the format string `"{0:#,###.##"` in the question is missing the closing `}`. – Lance U. Matthews Sep 26 '22 at 17:04

1 Answers1

-1

This is what worked for me

//Convert string to double then add currency formatting.                  
amount = getAmount.ToDouble();                          
var expectedAmount = "£" + string.Format("{0:#,###.00}", Convert.ToDecimal(amount));
Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40
RShome
  • 489
  • 2
  • 11
  • 35