Skip to content

Commit 646f458

Browse files
committed
Move appended strings to options array.
1 parent 4412563 commit 646f458

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

system/Helpers/number_helper.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ function number_to_size($num, int $precision = 1, string $locale=null)
8080
else
8181
{
8282
$unit = lang('Number.bytes');
83-
return number_format($num).' '.$unit;
8483
}
8584

86-
return format_number($num, $precision, $locale).' '.$unit;
85+
return format_number($num, $precision, $locale, ['after' => ' '.$unit]);
8786
}
8887
}
8988

@@ -147,7 +146,7 @@ function number_to_amount($num, int $precision, string $locale = null)
147146
$num = round(($num/1000), $precision);
148147
}
149148

150-
return format_number($num, $precision, $locale). $suffix;
149+
return format_number($num, $precision, $locale, ['after' => $suffix]);
151150
}
152151
}
153152

@@ -175,11 +174,25 @@ function format_number($num, int $precision = 1, string $locale = null, array $o
175174
// Try to format it per the locale
176175
$output = $formatter->format($num);
177176

177+
// This might lead a trailing period if $precision == 0
178+
$output = trim($output, '. ');
179+
178180
if (intl_is_failure($formatter->getErrorCode()))
179181
{
180182
throw new BadFunctionCallException($formatter->getErrorMessage());
181183
}
182184

185+
// Add on any before/after text.
186+
if (isset($options['before']) && is_string($options['before']))
187+
{
188+
$output = $options['before'].$output;
189+
}
190+
191+
if (isset($options['after']) && is_string($options['after']))
192+
{
193+
$output .= $options['after'];
194+
}
195+
183196
return $output;
184197
}
185198
}

tests/system/Helpers/NumberHelperTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ public function test_format_number()
1616
$this->assertEquals('123,456', format_number(123456, 0, 'en_US'));
1717
}
1818

19-
/**
20-
* @group single
21-
*/
2219
public function test_format_number_with_precision()
2320
{
2421
$this->assertEquals('123,456.8', format_number(123456.789, 1, 'en_US'));

0 commit comments

Comments
 (0)