Skip to content

Commit

Permalink
Add CultureInfo.InvariantCulture to floating point number formatting (#…
Browse files Browse the repository at this point in the history
…1541)

* Add CultureInfo.InvariantCulture to floating point number formatting

* Remove unused using statement in KubernetesYamlTests.cs
  • Loading branch information
tg123 committed Mar 27, 2024
1 parent 6bdc210 commit cca266e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/KubernetesClient/FloatEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions tests/KubernetesClient.Tests/KubernetesYamlTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string, double>() { ["hello"] = 10.01 });
Assert.Equal("hello: 10.01", yaml);
}
finally
{
CultureInfo.CurrentCulture = old;
}
}
}
}

0 comments on commit cca266e

Please sign in to comment.