1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//! Number formatting with explicit separators and grouping.
//!
//! You choose the decimal separator, the thousand separator, the group
//! size and how many decimal places to show.
//!
//! ```ignore
//! use rustango::numberformat::format;
//!
//! // en_US default — period decimal, no thousand sep
//! assert_eq!(format(1234.567, ".", None, 0, ""), "1234.567");
//!
//! // en_US with thousands grouping
//! assert_eq!(format(1234567.89, ".", Some(2), 3, ","), "1,234,567.89");
//!
//! // de_DE shape — comma decimal, period thousands
//! assert_eq!(format(1234567.89, ",", Some(2), 3, "."), "1.234.567,89");
//!
//! // fr_FR shape — comma decimal, non-breaking space thousands
//! assert_eq!(format(1234567.0, ",", Some(0), 3, "\u{00A0}"),
//! "1\u{00A0}234\u{00A0}567");
//!
//! // Indian numbering — groups of 3 then 2 — is not supported;
//! // grouping is uniform. Those apps need a custom grouping
//! // function.
//! ```
//!
//! ## Limits
//!
//! Values are `f64`, or `i64` through
//! [`format_i64`](crate::numberformat::format_i64). If you need
//! exact decimal maths, format the value yourself first and pass the
//! parts in.
/// Format a float.
///
/// `decimal_pos = None` keeps the number's natural precision;
/// `Some(n)` rounds to `n` decimal places. `grouping = 0` turns off
/// the thousand separator; a larger value inserts `thousand_sep`
/// every N digits, counting from the right.
///
/// A minus sign stays in front. NaN and infinity come back as
/// `"NaN"`, `"inf"` or `"-inf"` rather than an error, so a template
/// always has something to render.
#[must_use]
pub fn format(
value: f64,
decimal_sep: &str,
decimal_pos: Option<usize>,
grouping: usize,
thousand_sep: &str,
) -> String {
if !value.is_finite() {
return value.to_string();
}
let negative = value < 0.0;
let abs = value.abs();
let formatted = match decimal_pos {
Some(p) => format!("{abs:.p$}"),
None => {
let s = format!("{abs}");
// `Display` for f64 gives the shortest round-trip form, so
// "5" stays "5" and nothing is padded.
s
}
};
let (int_part, frac_part) = match formatted.split_once('.') {
Some((i, f)) => (i.to_owned(), Some(f.to_owned())),
None => (formatted, None),
};
let grouped = if grouping > 0 && !thousand_sep.is_empty() {
group_digits(&int_part, grouping, thousand_sep)
} else {
int_part
};
let mut out =
String::with_capacity(grouped.len() + frac_part.as_deref().map_or(0, str::len) + 4);
if negative {
out.push('-');
}
out.push_str(&grouped);
if let Some(frac) = frac_part {
out.push_str(decimal_sep);
out.push_str(&frac);
}
out
}
/// The `floatformat` filter. The sign of `precision` decides whether
/// trailing zeros are kept:
///
/// * `precision > 0`: exactly that many decimals, zeros kept.
/// * `precision < 0`: at most that many decimals, zeros dropped.
/// * `precision == 0`: no decimals, rounded to a whole number.
///
/// A negative precision is handy for prices: `-2` shows `$5` for a
/// round amount and `$5.50` otherwise.
///
/// ```
/// use rustango::numberformat::floatformat;
/// assert_eq!(floatformat(34.23234, -1), "34.2");
/// assert_eq!(floatformat(34.0, -1), "34");
/// assert_eq!(floatformat(34.23234, 3), "34.232");
/// assert_eq!(floatformat(34.0, 3), "34.000");
/// assert_eq!(floatformat(34.23234, -3), "34.232");
/// assert_eq!(floatformat(34.0, -3), "34");
/// ```
#[must_use]
pub fn floatformat(value: f64, precision: i64) -> String {
let abs = precision.unsigned_abs() as usize;
let drop_trailing = precision <= 0;
let formatted = format!("{value:.abs$}");
if drop_trailing {
if let Some((int_part, frac_part)) = formatted.split_once('.') {
if frac_part.chars().all(|c| c == '0') {
return int_part.to_owned();
}
}
}
formatted
}
/// Group an `i64`, with no float rounding to worry about.
#[must_use]
pub fn format_i64(value: i64, grouping: usize, thousand_sep: &str) -> String {
let negative = value < 0;
// `unsigned_abs` handles `i64::MIN` without overflowing.
let abs = value.unsigned_abs().to_string();
let grouped = if grouping > 0 && !thousand_sep.is_empty() {
group_digits(&abs, grouping, thousand_sep)
} else {
abs
};
if negative {
format!("-{grouped}")
} else {
grouped
}
}
/// Insert `sep` every `n` digits, counting from the right.
///
/// `digits` must already be digits only: the caller strips the sign
/// and the fractional part first.
pub(crate) fn group_digits(digits: &str, n: usize, sep: &str) -> String {
if digits.len() <= n {
return digits.to_owned();
}
let mut out = String::with_capacity(digits.len() + (digits.len() / n) * sep.len());
let first_group_len = digits.len() % n;
let mut i = 0;
if first_group_len > 0 {
// `digits` is ASCII, so byte slicing is fine.
out.push_str(&digits[..first_group_len]);
i = first_group_len;
}
while i < digits.len() {
if !out.is_empty() {
out.push_str(sep);
}
out.push_str(&digits[i..i + n]);
i += n;
}
out
}
#[cfg(test)]
mod tests {
use super::*;
// -------- format (f64) --------
#[test]
fn format_simple_no_grouping_no_decimal_round() {
assert_eq!(format(1234.567, ".", None, 0, ""), "1234.567");
}
#[test]
fn format_integer_value_no_decimal_part() {
// An integral float prints without a decimal point.
assert_eq!(format(100.0, ".", None, 0, ""), "100");
}
#[test]
fn format_with_fixed_decimal_pos() {
assert_eq!(format(1.5, ".", Some(2), 0, ""), "1.50");
assert_eq!(format(1.5, ".", Some(4), 0, ""), "1.5000");
}
#[test]
fn format_decimal_pos_rounds() {
// `1.555` is stored as `1.5549...`, so the exact result of a
// halfway case is not worth pinning. Just check the shape.
let out = format(1.555, ".", Some(2), 0, "");
assert!(out.starts_with("1.5"), "got: {out:?}");
assert_eq!(out.chars().count(), 4, "got: {out:?}");
// Values away from the halfway point round as expected.
assert_eq!(format(1.567, ".", Some(2), 0, ""), "1.57");
assert_eq!(format(1.234, ".", Some(2), 0, ""), "1.23");
}
#[test]
fn format_decimal_pos_zero_drops_fraction() {
assert_eq!(format(1234.7, ".", Some(0), 0, ""), "1235");
}
// -------- thousand-separator grouping --------
#[test]
fn format_thousands_grouping_en_us() {
assert_eq!(format(1_234_567.89, ".", Some(2), 3, ","), "1,234,567.89");
}
#[test]
fn format_thousands_grouping_de_de() {
// German: period thousands, comma decimal.
assert_eq!(format(1_234_567.89, ",", Some(2), 3, "."), "1.234.567,89");
}
#[test]
fn format_thousands_grouping_fr_fr_nbsp() {
// French: non-breaking space thousands, comma decimal.
assert_eq!(
format(1_234_567.0, ",", Some(0), 3, "\u{00A0}"),
"1\u{00A0}234\u{00A0}567"
);
}
#[test]
fn format_below_grouping_threshold() {
// < 1000: no separator inserted.
assert_eq!(format(999.0, ".", Some(0), 3, ","), "999");
}
#[test]
fn format_exactly_at_grouping_threshold() {
// Exactly 1000: one separator after the leading digit.
assert_eq!(format(1000.0, ".", Some(0), 3, ","), "1,000");
}
// -------- negatives --------
#[test]
fn format_negative_preserves_sign() {
assert_eq!(format(-1234.56, ".", Some(2), 3, ","), "-1,234.56");
assert_eq!(format(-100.0, ".", Some(0), 0, ""), "-100");
}
// -------- non-finite --------
#[test]
fn format_nan_uses_rust_display() {
assert_eq!(format(f64::NAN, ".", Some(2), 0, ""), "NaN");
}
#[test]
fn format_infinity_uses_rust_display() {
assert_eq!(format(f64::INFINITY, ".", Some(2), 0, ""), "inf");
assert_eq!(format(f64::NEG_INFINITY, ".", Some(2), 0, ""), "-inf");
}
// -------- empty thousand_sep disables grouping --------
#[test]
fn format_empty_thousand_sep_disables_grouping() {
// An empty separator disables grouping even when grouping=3.
assert_eq!(format(1234567.0, ".", Some(0), 3, ""), "1234567");
}
// -------- format_i64 --------
#[test]
fn format_i64_small_no_grouping() {
assert_eq!(format_i64(42, 0, ""), "42");
}
#[test]
fn format_i64_grouped() {
assert_eq!(format_i64(1_234_567, 3, ","), "1,234,567");
assert_eq!(format_i64(1_000, 3, "."), "1.000");
}
#[test]
fn format_i64_negative() {
assert_eq!(format_i64(-1_234_567, 3, ","), "-1,234,567");
assert_eq!(format_i64(-1, 3, ","), "-1");
}
#[test]
fn format_i64_zero() {
assert_eq!(format_i64(0, 3, ","), "0");
}
#[test]
fn format_i64_min_does_not_panic() {
// The `i64::MIN` edge case.
let s = format_i64(i64::MIN, 3, ",");
assert!(s.starts_with('-'));
assert!(s.contains(','));
}
// -------- group_digits internal helper --------
#[test]
fn group_digits_canonical_examples() {
assert_eq!(group_digits("1234567", 3, ","), "1,234,567");
assert_eq!(group_digits("1234567", 3, "."), "1.234.567");
assert_eq!(group_digits("100", 3, ","), "100");
assert_eq!(group_digits("1000", 3, ","), "1,000");
assert_eq!(group_digits("999999999", 3, ","), "999,999,999");
}
#[test]
fn group_digits_empty_input() {
assert_eq!(group_digits("", 3, ","), "");
}
#[test]
fn group_digits_smaller_than_one_group() {
assert_eq!(group_digits("12", 3, ","), "12");
}
// -------- floatformat --------
#[test]
fn floatformat_default_precision_drops_zero_decimal() {
assert_eq!(floatformat(34.23234, -1), "34.2");
assert_eq!(floatformat(34.0, -1), "34");
assert_eq!(floatformat(34.5, -1), "34.5");
}
#[test]
fn floatformat_positive_precision_keeps_trailing_zeros() {
assert_eq!(floatformat(34.23234, 3), "34.232");
assert_eq!(floatformat(34.0, 3), "34.000");
assert_eq!(floatformat(34.5, 2), "34.50");
}
#[test]
fn floatformat_negative_precision_drops_trailing_zeros() {
// Up to |N| decimals, trailing zeros dropped.
assert_eq!(floatformat(34.23234, -3), "34.232");
assert_eq!(floatformat(34.0, -3), "34");
assert_eq!(floatformat(5.5, -2), "5.50");
assert_eq!(floatformat(5.0, -2), "5");
}
#[test]
fn floatformat_zero_precision_no_decimals() {
// precision=0 gives no decimals. Exact halves round to even.
assert_eq!(floatformat(34.4, 0), "34");
assert_eq!(floatformat(34.6, 0), "35");
assert_eq!(floatformat(34.0, 0), "34");
}
#[test]
fn floatformat_rounds_to_nearest() {
// Standard f64 rounding.
assert_eq!(floatformat(1.234, 2), "1.23");
assert_eq!(floatformat(1.236, 2), "1.24");
}
#[test]
fn floatformat_negative_values() {
assert_eq!(floatformat(-34.5, -1), "-34.5");
assert_eq!(floatformat(-34.0, -1), "-34");
assert_eq!(floatformat(-1.2345, 2), "-1.23");
}
}