Skip to content

Commit fb44efa

Browse files
committed
[Bug 14546] com.livecode.arithmetic: Improve number->string format
Improve the `_ formatted as string` format for number types, by using the `%i` format operation when the number *can* be an integer and the `%g` operator otherwise.
1 parent 3efad74 commit fb44efa

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

libscript/src/module-arithmetic.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,21 @@ extern "C" MC_DLLEXPORT_DEF void MCArithmeticEvalNotEqualToNumber(MCNumberRef p_
452452
extern "C" MC_DLLEXPORT_DEF MCStringRef MCArithmeticExecFormatNumberAsString(MCNumberRef p_operand)
453453
{
454454
MCAutoStringRef t_output;
455-
MCStringFormat(&t_output, "%f", MCNumberFetchAsReal(p_operand));
455+
if (MCNumberIsInteger(p_operand))
456+
{
457+
if (!MCStringFormat(&t_output, "%i", MCNumberFetchAsInteger(p_operand)))
458+
{
459+
return nil;
460+
}
461+
}
462+
else
463+
{
464+
if (!MCStringFormat(&t_output, "%g", MCNumberFetchAsReal(p_operand)))
465+
{
466+
return nil;
467+
}
468+
}
469+
456470
return MCValueRetain(*t_output);
457471
}
458472

tests/lcb/stdlib/arithmetic.lcb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,9 @@ public handler TestLessThanEqual()
173173
end handler
174174

175175
public handler TestFormatString()
176-
test diagnostic "TODO -1 formatted as string (bug 14594)"
177-
broken test "format as string (int)" when (-1) formatted as string is "-1" because "bug 14546"
176+
test "format as string (int)" when (-1) formatted as string is "-1"
178177

179-
variable tNum
180-
put (-1.0) formatted as string into tNum
181-
test diagnostic "TODO test full string, not just prefix"
182-
test diagnostic tNum
183-
test "format as string (real)" when tNum begins with "-1."
178+
test "format as string (real)" when (-1.1) formatted as string is "-1.1"
184179
end handler
185180

186181
public handler TestParseString()

0 commit comments

Comments
 (0)