System.Convert.ToSingle (), (1.5) vs (1,5)

I am writing a C # program to convert model formats. The model format has numbers in the form of text, such as "-0.136222".

I can use System.Convert.ToSingle () to convert this number to a floating point number. But here in Germany, we use commas as decimal points (-0.136222), and System.Convert chooses this. Now I have a problem of not recognizing decimal points, since it expects commas.

In a nutshell; We have the following: "-0.136222" We get the following: -0136222.0f because it expects this: "-0.136222"

Can I tell the system to recognize commas as decimal points only for my program? Work around will not work, because it must be portable (to other countries).

+2
source share
2 answers

Use Single.Parse () instead, for example:

Single.Parse("-0.136222", CultureInfo.InvariantCulture);

InvariantCulture is a way to tell a parser to a string, ignoring individual decimal separators and grouping separators.

+2
source

.. Framework.Net, , , . , , float , , , , .

, Culture .Net CultureInfo.InvariantCulture ( Pawel ) CultureInfo, , . :)


. , () , , , float , , , .

0

Source: https://habr.com/ru/post/1545768/


All Articles