3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[小数点]スペインでは1.2が12になることもある[TryParse]

Last updated at Posted at 2025-05-19

結論から言うと、float.TryParse()等で文字列操作する時は、最初に

System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

を宣言しておくと、どの地域でも1.2を1.2のまま読み取ることができます。


AssetStoreで販売中のアセットについて質問が来ました。
「コレが、スクリーンショット 2025-05-19 223152.png
うちだとこうなるんですけど」
スクリーンショット 2025-05-19 223013.png

どうやら欧州などでは 1.21,2 と表記する地域があり、
StringReader を使用して.csvファイルを読み込むと、 float.TryParse()で 1.2 -> 12 のように変換されてしまうみたいなんですね。

.CSVデータ

dummyData.csv
//name,value1,value2
data1,12,34
data2,1.2,3.4
data3,"1,2","3,4"
StringReaderサンプル
DecimapPointRegionTest.cs
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
namespace DecimalPointRegionDemo
{
    public class DecimapPointRegionTest : MonoBehaviour
    {
        [SerializeField] TextAsset m_csvFile;

        // Start is called once before the first execution of Update after the MonoBehaviour is created
        void Start()
        {
            Debug.Log("----- Current culture -----");
            OnReadCsvFile(m_csvFile);
            Debug.Log("----- Spanish culture -----");
            System.Globalization.CultureInfo.CurrentCulture = new System.Globalization.CultureInfo("es-ES");
            OnReadCsvFile(m_csvFile);
            Debug.Log("----- InvariantCulture -----");
            System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
            OnReadCsvFile(m_csvFile);
        }

        // Update is called once per frame
        void Update()
        {

        }

        void OnReadCsvFile(TextAsset _csvFile)
        {
            Regex reg = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
            StringReader reader = new StringReader(_csvFile.text);
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                string[] values = reg.Split(line); // ""で囲まれたものは分割しない
                if (values[0].StartsWith("//")) continue; // Skip comment line
                // Assuming the first column is a string and the second column is a float
                string name = values[0].Trim('"');
                if (float.TryParse(values[1].Trim('"'), out float value1))
                    if (float.TryParse(values[2].Trim('"'), out float value2))
                        Debug.Log($"Name: {name}, Value1: {value1}, Value2: {value2}");
            }
        }
    }
}

結果
image.png

CultureInfoがスペインの時は
1.1 -> 11
1,1 -> 1.1 (表記上は1,1)
になっていますね。
System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
を宣言しておくことで、この問題を回避することができました。

スクリーンショット 2025-05-19 235034.png

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?