Skip to content

Commit eb30ddd

Browse files
committed
Added a test for an edge case of float formatting and fixed a bug.
1 parent 40a87a7 commit eb30ddd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Assets/DebugOverlay/TextFormatter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ void IConverter<float>.Convert(ref char* dst, char* end, float value, FormatSpec
4343
}
4444
int v1 = Mathf.FloorToInt(value);
4545
float fractMult = (int)Mathf.Pow(10.0f, formatSpec.fractWidth);
46-
int v2 = Mathf.RoundToInt(value * fractMult) % (int)(fractMult);
46+
int v2 = Mathf.RoundToInt((value-(float)v1) * fractMult);
47+
if(v2>=fractMult) {
48+
v1++;
49+
v2 = 0;
50+
}
4751
ConvertInt(ref dst, end, neg ? -v1 : v1, intWidth, formatSpec.integerWidth, formatSpec.leadingZero);
4852
if (dst < end)
4953
*dst++ = '.';

Assets/DebugOverlay/TextFormatterTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public void Formats_Float_CustomPrecision()
4343
Assert.AreEqual("Value: 03.142", new string(buf, 0, len));
4444
}
4545

46+
[Test]
47+
public void Formats_Float_CustomPrecision_2()
48+
{
49+
char[] buf = new char[32];
50+
int len = StringFormatter.Write(ref buf, 0, "Value: {0:0.000}", 9.9995f);
51+
Assert.AreEqual("Value: 10.000", new string(buf, 0, len));
52+
}
53+
4654
[Test]
4755
public void Formats_String_LeftAlign()
4856
{

0 commit comments

Comments
 (0)