Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/kOS.Safe.Test/Structures/StringValueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,16 @@ public void CanParseScientificStrings()
stringTest = new StringValue(" 1.23e+3a ");
Assert.Throws(typeof(Exceptions.KOSNumberParseException), () => stringTest.ToScalar());
}

[Test]
public void CanFormat()
{
var formatString = new StringValue("test1={0:0.0} test2='{0,10:0.0}'");

var expected = new StringValue("test1=13.4 test2=' 13.4'");
var actual = formatString.Format(new ScalarDoubleValue(13.37));

Assert.That(expected.ToString(), Is.EqualTo(actual.ToString()));
}
}
}
17 changes: 16 additions & 1 deletion src/kOS.Safe/Encapsulation/StringValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using kOS.Safe.Serialization;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

namespace kOS.Safe.Encapsulation
{
Expand Down Expand Up @@ -257,7 +258,21 @@ public StringValue Format(params Structure[] args)
{
if (args.Length == 0)
return this;
return new StringValue(string.Format(CultureInfo.InvariantCulture, this, args));

// Unwrap the primitive scalar value and send them
// into String.Format as-is. This is required to allow
// for numeric formatting patterns, like rounding.
var primitiveArgs = args
.Select(arg => {
if (arg is ScalarValue scalar) {
return scalar.ToPrimitive();
}

return arg;
})
.ToArray();

return new StringValue(string.Format(CultureInfo.InvariantCulture, this, primitiveArgs));
}

private void StringInitializeSuffixes()
Expand Down
Loading