0

I have tried string.replace("R","").... (",","") then converting to or parsing, doulbe.tryparse, convertoDouble as well as system.globalization.numberstyles, currency.

The the string R160,000 on localhost works perfect. on web app it says the string format is wrong.

on localhost the string is 160000 which works fine.

on the webapp, The string ends up as 160 000 which does not work.

nilsK
  • 4,323
  • 2
  • 26
  • 40
Gee
  • 1
  • 4
  • 2
    We cannot help until you [post the code](http://idownvotedbecau.se/nocode/) you are having trouble with. – Dour High Arch Feb 05 '19 at 16:39
  • 1
    'it says the string format is wrong' who is 'it'? And how does it say that? Please be more precise. We dont know your code. Add your code and Exception/Error. – nilsK Feb 05 '19 at 16:42
  • double.tryparse("R160,000", out double).... double = convert.todouble(R160,000)... R160,000 = R160,000.Replace("R","") R160,000 = R160,000.Replace(",","").... R160,000 = R160,000.Replace(" ","") R160,000 = R160,000.Replace("_","") THEN tryparse... or convert.todouble – Gee Feb 05 '19 at 17:32
  • 2
    Put your code [in your question](https://stackoverflow.com/posts/54539070/edit). Do not try to post code in comments. – Dour High Arch Feb 05 '19 at 18:01

1 Answers1

2

Your local machine probably has the culture set to match the string you are trying to parse and the web app is running with a different default culture. You need to pass in the culture info that matches the string you are trying to parse.

For example:

var s = "R1000";
CultureInfo cInfo = new CultureInfo("af-ZA", false);
double.TryParse(s, NumberStyles.Currency, cInfo, out double result);
Brad Patton
  • 4,007
  • 4
  • 34
  • 44
  • 1
    Please [use `decimal` for currency](https://stackoverflow.com/questions/3730019/), not `double`. – Dour High Arch Feb 05 '19 at 17:30
  • Thanks so much for the help. The great thing is that its not giving me the error anymore, however, the tryparse is producing a "0" from the string R160,000.00 – Gee Feb 05 '19 at 17:31
  • @Gee The string `R160,000.00` isn't valid in the `'af-ZA'` CultureInfo. Double check `cInfo.NumberFormat.CurrencyDecimalSeparator` (a comma) and `cInfo.NumberFormat.CurrencyGroupSeparator` (a space / No-Break Space). Try e.g. `var s = "R160 000,00";` – JosefZ Feb 05 '19 at 20:20
  • Thanks Guys. You are magic. Really really really really appreciated. – Gee Feb 06 '19 at 06:02