-2

I am new to C # and I am currently having problems with the following. In C #, I have a Pi floating point number and I want to convert it to a string using the ToString() method. But the conversion gives a string result with a comma "3,1415". On another machine, the same gives the string result with the dot "3.1415". What is the reason for this and what should I do to get a dotted string result?

EDIT: The problem is, I can't change the code, but I can install and uninstall .Net frameworks, change my OS settings, etc.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Vahag Chakhoyan
  • 873
  • 1
  • 10
  • 21
  • Does this answer your question? [Converting double to string with N decimals, dot as decimal separator, and no thousand separator](https://stackoverflow.com/questions/4076789/converting-double-to-string-with-n-decimals-dot-as-decimal-separator-and-no-th) – GSerg Jul 09 '21 at 13:17
  • Without seeing your code, it's actually a little hard to tell what's wrong. However, it's probably related with OS language/regional settings – Cid Jul 09 '21 at 13:17
  • 3
    The reason for this is the CultureInfo associated to the language of the machine. To obtain the same result you could do `.ToString(CultureInfo.InvariantCulture);`. You can find more info here: https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo?view=net-5.0 – oRoiDev Jul 09 '21 at 13:17
  • please share your code of converting. – Amit Verma Jul 09 '21 at 13:18
  • Pass the culture you want to `ToString`, eg `num.ToSting(CultureInfo.GetCultureInfo("en-US"))`, or `num.ToSting(CultureInfo.InvariantCulture)` – Panagiotis Kanavos Jul 09 '21 at 13:21
  • 1
    **Why** do you want to use a dot? It matters. Clearly, in you country, the decimal separator is `,`, not `.`. .NET will use the current user's locale both for parsing and formatting. The users of the application will expect it to respect their locale. Their files will use the separators specified by their locale. If you want to export data as eg CSV, then you may want to specify the locale explicitly through `CultureInfo` – Panagiotis Kanavos Jul 09 '21 at 13:26
  • 1
    `The problem is, I can't change the code,...` that's not the problem. You still haven't described what the problem is. If anything it looks like the code works as it should – Panagiotis Kanavos Jul 09 '21 at 13:27
  • I edited the question. I cannot change the code. – Vahag Chakhoyan Jul 09 '21 at 13:28
  • 1
    @VahagChakhoyan but what is the problem??? You still haven't described any problem to solve – Panagiotis Kanavos Jul 09 '21 at 13:30
  • What can I do to get the result as a dotted string without changing the code? – Vahag Chakhoyan Jul 09 '21 at 13:33
  • 3
    [**Why**](https://meta.stackexchange.com/q/66377/147640) do you want to get that result @VahagChakhoyan? On *your machine only*, without changing the code, you can go the Windows regional settings and change the decimal separator there. But why? – GSerg Jul 09 '21 at 13:35
  • 1
    Why do you assume you need that in the first place? Why do you assume you should display numbers in any format other than the one selected by the end user? That's a **bug** not a feature. If you display `1.000` what would people in your country read? One or a Thousand? – Panagiotis Kanavos Jul 09 '21 at 13:35
  • 1
    Do you have a desktop or web application? Do you care about display or do you want to export data in a specific format? If it's just for display in a desktop application, there's nothing to change. The application is already displaying numbers in the format requested by the user. – Panagiotis Kanavos Jul 09 '21 at 13:37
  • 1
    Everyone except developers in your country will expect commas. It's only us developers that change the locale to US because we don't like the OS translations. The application sounds to be working just fine. It's only that other machine that has a US locale. – Panagiotis Kanavos Jul 09 '21 at 13:39

4 Answers4

-1

Edit: if you can't change the code. Change the language/localization of the system to one which uses dot as decimal separator. In Control Panel or Settings.

You should look at internationalization and localization in the System.Globalization namespace.

The advice here is to use one CultureInfo specific for parsing numbers or writing numbers to string.

var flt = 232.23f;

var str = flt.ToString(CultureInfo.InvariantCulture); //for example, you can use CultureInfo.CurrentCulture

This allows you to keep the ThreadCulture without change it.

But take a look at this link https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo?view=net-5.0 .Take your time, is dense.

DrkDeveloper
  • 939
  • 7
  • 17
-2

I would just set the current culture at the entry point of your program.

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");

However I would also check the regional settings and so forth on the machine where the comma separator appears.

Alan B
  • 4,086
  • 24
  • 33
-2

It is related with current culture info. You can specify the culture info in ToString method as a parameter like;

var convertedFloat = floatVariable.ToString(new CultureInfo("en-GB"));
-2

Thanks to GSerg, for comment about Windows regional settings. That solves my problem. In the Windows Control panel enter Region and Language. In the Formats tab click Additional Settings and in the Decimal symbol field specify what decimal separator must be used when converting a floating point number to a string.

Vahag Chakhoyan
  • 873
  • 1
  • 10
  • 21