File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -219,5 +219,16 @@ public void CanParseScientificStrings()
219219 stringTest = new StringValue ( " 1.23e+3a " ) ;
220220 Assert . Throws ( typeof ( Exceptions . KOSNumberParseException ) , ( ) => stringTest . ToScalar ( ) ) ;
221221 }
222+
223+ [ Test ]
224+ public void CanFormat ( )
225+ {
226+ var formatString = new StringValue ( "test1={0:0.0} test2='{0,10:0.0}'" ) ;
227+
228+ var expected = new StringValue ( "test1=13.4 test2=' 13.4'" ) ;
229+ var actual = formatString . Format ( new ScalarDoubleValue ( 13.37 ) ) ;
230+
231+ Assert . That ( expected . ToString ( ) , Is . EqualTo ( actual . ToString ( ) ) ) ;
232+ }
222233 }
223234}
Original file line number Diff line number Diff line change 77using kOS . Safe . Serialization ;
88using System . Collections . Generic ;
99using System . Collections ;
10+ using System . Linq ;
1011
1112namespace kOS . Safe . Encapsulation
1213{
@@ -257,7 +258,21 @@ public StringValue Format(params Structure[] args)
257258 {
258259 if ( args . Length == 0 )
259260 return this ;
260- return new StringValue ( string . Format ( CultureInfo . InvariantCulture , this , args ) ) ;
261+
262+ // Unwrap the primitive scalar value and send them
263+ // into String.Format as-is. This is required to allow
264+ // for numeric formatting patterns, like rounding.
265+ var primitiveArgs = args
266+ . Select ( arg => {
267+ if ( arg is ScalarValue scalar ) {
268+ return scalar . ToPrimitive ( ) ;
269+ }
270+
271+ return arg ;
272+ } )
273+ . ToArray ( ) ;
274+
275+ return new StringValue ( string . Format ( CultureInfo . InvariantCulture , this , primitiveArgs ) ) ;
261276 }
262277
263278 private void StringInitializeSuffixes ( )
You can’t perform that action at this time.
0 commit comments