From cca266e6aef1ca8bef8ce44f9823302c1f601d1f Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Wed, 27 Mar 2024 15:30:26 -0700 Subject: [PATCH] Add CultureInfo.InvariantCulture to floating point number formatting (#1541) * Add CultureInfo.InvariantCulture to floating point number formatting * Remove unused using statement in KubernetesYamlTests.cs --- src/KubernetesClient/FloatEmitter.cs | 5 +++-- .../KubernetesYamlTests.cs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/KubernetesClient/FloatEmitter.cs b/src/KubernetesClient/FloatEmitter.cs index 8f5fa947..4a0f1851 100644 --- a/src/KubernetesClient/FloatEmitter.cs +++ b/src/KubernetesClient/FloatEmitter.cs @@ -1,3 +1,4 @@ +using System.Globalization; using YamlDotNet.Core; using YamlDotNet.Core.Events; using YamlDotNet.Serialization; @@ -18,10 +19,10 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter) { // Floating point numbers should always render at least one zero (e.g. 1.0f => '1.0' not '1') case double d: - emitter.Emit(new Scalar(d.ToString("0.0######################"))); + emitter.Emit(new Scalar(d.ToString("0.0######################", CultureInfo.InvariantCulture))); break; case float f: - emitter.Emit(new Scalar(f.ToString("0.0######################"))); + emitter.Emit(new Scalar(f.ToString("0.0######################", CultureInfo.InvariantCulture))); break; default: base.Emit(eventInfo, emitter); diff --git a/tests/KubernetesClient.Tests/KubernetesYamlTests.cs b/tests/KubernetesClient.Tests/KubernetesYamlTests.cs index 261e0819..9f14f73a 100644 --- a/tests/KubernetesClient.Tests/KubernetesYamlTests.cs +++ b/tests/KubernetesClient.Tests/KubernetesYamlTests.cs @@ -1,6 +1,7 @@ using k8s.Models; using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Text; using System.Threading.Tasks; @@ -1095,5 +1096,21 @@ public void LoadFromStringCRDMerge() Assert.Equal("v1beta2", crd.Spec.Versions[0].Name); Assert.Equal("v1", crd.Spec.Versions[1].Name); } + + [Fact] + public void NoGlobalization() + { + var old = CultureInfo.CurrentCulture; + try + { + CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-fr"); + var yaml = KubernetesYaml.Serialize(new Dictionary() { ["hello"] = 10.01 }); + Assert.Equal("hello: 10.01", yaml); + } + finally + { + CultureInfo.CurrentCulture = old; + } + } } }