1

Is there anyway in C# to change "." to ","? I live in Belgium and here we use a comma instead of a dot. When I type for example 1,1 C# says its not a number. It only accepts 1.1 or 1.

Here's my cshtml code:

<div class="form-group">
    <label asp-for="preSeriousness" class="control-label">Pre seriousness:</label>
    <input asp-for="preSeriousness" class="form-control" type="text" required />
    <span asp-validation-for="preSeriousness" class="text-danger"></span>
</div>

Here's my controller code:

preSeriousness = Convert.ToDouble(collection["preSeriousness"]),

Here's code from my start.cs configure method:

var cultureInfo = new CultureInfo("nl-BE");
cultureInfo.NumberFormat.CurrencySymbol = "€";
cultureInfo.NumberFormat.NumberDecimalSeparator = ",";
cultureInfo.NumberFormat.CurrencyDecimalSeparator = ",";

CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

When I run this code it writes nl-BE:

CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
Console.WriteLine(cultureInfo);

Here's an image to make it more clear:

enter image description here

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
A.Vandijck
  • 59
  • 1
  • 8
  • 1
    You might find an answer here: [How to: Set a Culture for an ASP.NET Web Page](https://docs.devexpress.com/AspNet/12062/localization/how-to-set-a-culture-for-an-aspnet-web-page) – Jeroen van Langen May 03 '20 at 21:51
  • 1
    We normally use CurrentCulture and CurrentUICulture for converting string input to numeric types. If you are running your application with your local culture, why do you specify InvariantCulture while converting to double? Invariant culture uses `.` as decimal seperator – Oguz Ozgul May 03 '20 at 21:51
  • Does this answer your question? [Set CultureInfo in Asp.net Core to have a . as CurrencyDecimalSeparator instead of ,](https://stackoverflow.com/questions/40442444/set-cultureinfo-in-asp-net-core-to-have-a-as-currencydecimalseparator-instead) – Guru Stron May 03 '20 at 22:08
  • I tried the answer of that question but without result, did post what i tried @GuruStron – A.Vandijck May 03 '20 at 22:31
  • What culture is set on your computer/browser? – Guru Stron May 03 '20 at 22:59
  • Culture of my computer is nl-BE, same for my browser as far as i can see. Added some code to see what culture im currently using and it returns nl-BE as i have set.@GuruStron – A.Vandijck May 03 '20 at 23:05
  • Well, if i tried CurrentCulture and CurrentUICulture but i still get the message that 1,1 is not a number.. @OguzOzgul – A.Vandijck May 03 '20 at 23:16
  • Can you please try to see the value of `NumberFormatInfo.CurrentInfo.NumberDecimalSeparator` ? `Convert.ToDouble(string) => double.Parse(string)` uses `NumberFormatInfo.CurrentInfo` – Oguz Ozgul May 04 '20 at 00:54
  • Can you please also `Console.WriteLine(collection["preSeriousness"])` ? Because even with the invariant culture, `1,1` is converted to `11` (does not throw) and is a number. There must be something wrong with the value! – Oguz Ozgul May 04 '20 at 01:02
  • So this ```NumberFormatInfo.CurrentInfo.NumberDecimalSeparator``` returned "," @OguzOzgul – A.Vandijck May 04 '20 at 11:36
  • And this ```string preSeriousness = (collection["preSeriousness"]); Console.WriteLine(preSeriousness);``` returned "1.1" when i did put in 1.1 – A.Vandijck May 04 '20 at 11:38
  • The problem is that the textbox does not allow me to put in "1,1" as you can see in the image above. So i can't even submit the form with "1,1" filled in. – A.Vandijck May 04 '20 at 11:39
  • Then the problem is on the client side and all we look for is the server side. I am going to reproduce the problem on my Mvc test site. I live in Turkey so we also use `,` for decimal seperator – Oguz Ozgul May 04 '20 at 11:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213080/discussion-between-a-vandijck-and-oguz-ozgul). – A.Vandijck May 04 '20 at 11:44

0 Answers0