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
369
370
371
372
373
374
375
376
377
378
379
380
///
/// Copyright [2022] DiarioBitcoin.com, Angel Leon
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// https://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///

/// ## Customize the separator characters and decimal precision used to format the output of the given `f64` number
///
/// ## Parameters:
/// **amount: f64**: the original `amount` in `f64`
///
/// **max_decimal_places: u16**: number of decimal digits to keep for `amounts > 0`
///
/// **zero_comma_decimal_places: u16**: number of decimal digits to keep for `amount < 1`
///
/// **thousands_separator: char**: character to be used to separate thousands on the integer part of `amount`
///
/// **radix_character: char**: character that separates the integer portion from the decimal portion (e.g. `.` for numbers in US locale but `,` for numbers in Spanish locales)
///
/// **decimal_separator: char**: character that separates decimal portions every thousandth
pub fn endinero_f64(
    amount: f64,
    max_decimal_places: u16,
    zero_comma_decimal_places: u16,
    thousands_separator: char,
    radix_character: char,
    decimals_separator: char,
) -> String {
    let int_part = integer_part_f64(amount, thousands_separator);
    let total_decimals = if amount.abs() < 1.0f64 {
        zero_comma_decimal_places
    } else {
        max_decimal_places
    };
    let dec_part = decimal_part_f64(amount, total_decimals, decimals_separator);
    format!("{}{}{}", int_part, radix_character, dec_part)
}

/// ## Customize the separator characters and decimal precision used to format the output of the given `f32` number
///
/// ## Parameters:
/// **amount: f64**: the original `amount` in `f32`
///
/// **max_decimal_places: u16**: number of decimal digits to keep for `amounts > 0`
///
/// **zero_comma_decimal_places: u16**: number of decimal digits to keep for `amount < 1`
///
/// **thousands_separator: char**: character to be used to separate thousands on the integer part of `amount`
///
/// **radix_character: char**: character that separates the integer portion from the decimal portion (e.g. `.` for numbers in US locale but `,` for numbers in Spanish locales)
///
/// **decimal_separator: char**: character that separates decimal portions every thousandth
pub fn endinero_f32(
    amount: f32,
    max_decimal_places: u16,
    zero_comma_decimal_places: u16,
    thousands_separator: char,
    radix_character: char,
    decimals_separator: char,
) -> String {
    // INTEGER PART
    let int_part = integer_part_f32(amount, thousands_separator);
    let total_decimals = if amount.abs() < 1.0 {
        zero_comma_decimal_places
    } else {
        max_decimal_places
    };
    // DECIMAL PART
    let dec_part = decimal_part_f32(amount, total_decimals, decimals_separator);
    // RESULT
    format!("{}{}{}", int_part, radix_character, dec_part)
}

/// Format `f64` input into a spanish money format, max 2 decimal places if amount > 0, up to 17 decimal places for amount < 1. Thousands separator: `.`, Radix character: `,`, Decimals separator: ` `
pub fn dinero_f64(amount: f64) -> String {
    endinero_f64(amount, 2, 17, '.', ',', ' ')
}

/// Format `f32` input into a spanish money format, max 2 decimal places if amount > 0, up to 17 decimal places for amount < 1. Thousands separator: `.`, Radix character: `,`, Decimals separator: ` `
pub fn dinero_f32(amount: f32) -> String {
    endinero_f32(amount, 2, 7, '.', ',', ' ')
}

/// Format `f64` input into a US money format, max 2 decimal places if amount > 0, up to 17 decimal places for amount < 1. Thousands separator: `,`, Radix character: `.`, Decimals separator: ` `
pub fn money_f64(amount: f64) -> String {
    endinero_f64(amount, 2, 17, ',', '.', ' ')
}

/// Format `f32` input into a US money format, max 2 decimal places if amount > 0, up to 17 decimal places for amount < 1. Thousands separator: `,`, Radix character: `.`, Decimals separator: ` `
pub fn money_f32(amount: f64) -> String {
    endinero_f64(amount, 2, 7, ',', '.', ' ')
}

fn integer_part_f64(amount: f64, thousands_separator: char) -> String {
    let int_part = amount.abs().trunc() as i64;
    let unsigned_int_digits_str = format!("{}", int_part);
    let mut formatted_int_digits: Vec<char> = Vec::new();
    let mut digits_added = 0;
    for c in unsigned_int_digits_str.chars().rev() {
        if digits_added > 0 && digits_added % 3 == 0 {
            formatted_int_digits.push(thousands_separator.clone());
        }
        formatted_int_digits.push(c);
        digits_added += 1;
    }
    if amount < 0f64 {
        formatted_int_digits.push('-');
    }
    formatted_int_digits.reverse();
    let result = formatted_int_digits.iter().collect::<String>();
    result
}

fn integer_part_f32(amount: f32, thousands_separator: char) -> String {
    let int_part = amount.abs().trunc() as i32;
    let unsigned_int_digits_str = int_part.to_string();//format!("{}", int_part);
    let mut formatted_int_digits: Vec<char> = Vec::new();
    let mut digits_added = 0;
    for c in unsigned_int_digits_str.chars().rev() {
        if digits_added > 0 && digits_added % 3 == 0 {
            formatted_int_digits.push(thousands_separator.clone());
        }
        formatted_int_digits.push(c);
        digits_added += 1;
    }
    if amount < 0f32 {
        formatted_int_digits.push('-');
    }
    formatted_int_digits.reverse();
    let result = formatted_int_digits.iter().collect::<String>();
    result
}

fn decimal_part_f64(amount: f64, total_decimals: u16, decimals_separator: char) -> String {
    let dec_digits_str = format!("{:.17}", amount).split(".").collect::<Vec<&str>>().get(1).unwrap().to_string();
    let mut formatted_dec_digits: Vec<char> = Vec::new();
    let mut digits_added = 0;
    for c in dec_digits_str.chars() {
        if digits_added > 0 && digits_added % 3 == 0 {
            formatted_dec_digits.push(decimals_separator);
        }
        formatted_dec_digits.push(c);
        digits_added += 1;

        if digits_added == total_decimals {
            break;
        }
    }

    // remove any zeroes or blank spaces left over at the end
    while formatted_dec_digits.len() > 1 &&
        formatted_dec_digits.last().is_some() &&
        (formatted_dec_digits.last().unwrap() == &'0' ||
            formatted_dec_digits.last().unwrap() == &' ') {
        formatted_dec_digits.pop();
    }

    let result = formatted_dec_digits.iter().collect::<String>();
    result
}

fn decimal_part_f32(amount: f32, total_decimals: u16, decimals_separator: char) -> String {
    let dec_digits_str = format!("{:.7}", amount).split(".").collect::<Vec<&str>>().get(1).unwrap().to_string();
    let mut formatted_dec_digits: Vec<char> = Vec::new();
    let mut digits_added = 0;
    for c in dec_digits_str.chars() {
        if digits_added > 0 && digits_added % 3 == 0 {
            formatted_dec_digits.push(decimals_separator);
        }
        formatted_dec_digits.push(c);
        digits_added += 1;

        if digits_added == total_decimals {
            break;
        }
    }
    // remove any zeroes or blank spaces left over at the end
    while
    formatted_dec_digits.len() > 1 &&
        formatted_dec_digits.last().is_some() &&
        (formatted_dec_digits.last().unwrap() == &'0' ||
            formatted_dec_digits.last().unwrap() == &' ') {
        formatted_dec_digits.pop();
    }

    let result = formatted_dec_digits.iter().collect::<String>();
    result
}

#[test]
fn integer_part_tests() {
    // Zeroes
    assert_eq!(integer_part_f64(0.0, '.'), "0");
    assert_eq!(integer_part_f64(-0.0, '.'), "0");
    assert_eq!(integer_part_f64(-0.0001, '.'), "-0");

    // Units [1-9]
    assert_eq!(integer_part_f64(1.0, '.'), "1");
    assert_eq!(integer_part_f64(-1.0, '.'), "-1");

    // Tens
    assert_eq!(integer_part_f64(12.0, '.'), "12");
    assert_eq!(integer_part_f64(-12.0, '.'), "-12");

    // Hundreds
    assert_eq!(integer_part_f64(123.0, '.'), "123");
    assert_eq!(integer_part_f64(-123.0, '.'), "-123");

    // Thousands
    assert_eq!(integer_part_f64(1234.0, '.'), "1.234");
    assert_eq!(integer_part_f64(-1234.0, '.'), "-1.234");

    // Tens of thousands
    assert_eq!(integer_part_f64(12345.0, '.'), "12.345");
    assert_eq!(integer_part_f64(-12345.0, '.'), "-12.345");

    // Hundreds of thousands
    assert_eq!(integer_part_f64(123456.0, '.'), "123.456");
    assert_eq!(integer_part_f64(-123456.0, '.'), "-123.456");

    // Millions
    assert_eq!(integer_part_f64(1234567.0, '.'), "1.234.567");
    assert_eq!(integer_part_f64(-1234567.0, '.'), "-1.234.567");

    // Tens of millions
    assert_eq!(integer_part_f64(12345678.0, '.'), "12.345.678");
    assert_eq!(integer_part_f64(-12345678.0, '.'), "-12.345.678");

    // Hundreds of millions
    assert_eq!(integer_part_f64(123456789.0, '.'), "123.456.789");
    assert_eq!(integer_part_f64(-123456789.0, '.'), "-123.456.789");

    // Billions / Millardos
    assert_eq!(integer_part_f64(1234567890.0, '.'), "1.234.567.890");
    assert_eq!(integer_part_f64(-1234567890.0, '.'), "-1.234.567.890");

    // Tens of billions / Decenas de millardos
    assert_eq!(integer_part_f64(12345678901.0, '.'), "12.345.678.901");
    assert_eq!(integer_part_f64(-12345678901.0, '.'), "-12.345.678.901");

    // Hundreds of billions / Cientos de millardos
    assert_eq!(integer_part_f64(123456789012.0, '.'), "123.456.789.012");
    assert_eq!(integer_part_f64(-123456789012.0, '.'), "-123.456.789.012");

    // Trillions (10^12) / Billones
    assert_eq!(integer_part_f64(1234567890123.0, '.'), "1.234.567.890.123");
    assert_eq!(
        integer_part_f64(-1234567890123.0, '.'),
        "-1.234.567.890.123"
    );

    // Tens of trillions (10^13) / Decenas de billones
    assert_eq!(
        integer_part_f64(12345678901234.0, '.'),
        "12.345.678.901.234"
    );
    assert_eq!(
        integer_part_f64(-12345678901234.0, '.'),
        "-12.345.678.901.234"
    );

    // Hundreds of trillions (10^14) / Cientos de billones
    assert_eq!(
        integer_part_f64(123456789012345.0, '.'),
        "123.456.789.012.345"
    );
    assert_eq!(
        integer_part_f64(-123456789012345.0, '.'),
        "-123.456.789.012.345"
    );

    // Quadrillion (10^15) / Billardo
    assert_eq!(
        integer_part_f64(1234567890123456.0, '.'),
        "1.234.567.890.123.456"
    );
    assert_eq!(
        integer_part_f64(-1234567890123456.0, '.'),
        "-1.234.567.890.123.456"
    );
}

#[test]
fn decimal_part_tests() {
    assert_eq!(decimal_part_f64(0.1, 1, ' '), "1");
    assert_eq!(decimal_part_f64(0.12, 1, ' '), "1");

    assert_eq!(decimal_part_f64(0.12, 2, ' '), "12");
    assert_eq!(decimal_part_f64(0.123, 2, ' '), "12");

    assert_eq!(decimal_part_f64(0.123, 3, ' '), "123");
    assert_eq!(decimal_part_f64(0.1234, 3, ' '), "123");

    assert_eq!(decimal_part_f64(0.1234, 4, ' '), "123 4");
    assert_eq!(decimal_part_f64(0.1234, 4, '.'), "123.4");

    assert_eq!(decimal_part_f64(0.222333444555, 12, '.'), "222.333.444.555");
    assert_eq!(
        decimal_part_f64(0.2223334445556, 12, '.'),
        "222.333.444.555"
    );
    assert_eq!(
        decimal_part_f64(0.2223334445556, 13, '.'),
        "222.333.444.555.6"
    );

    assert_eq!(decimal_part_f32(0.2223334445556, 7, '.'), "222.333.4");
    // breaks after 7 digits, starts doing weird rounding
    assert_ne!(decimal_part_f32(0.22233344455566, 8, '.'), "222.333.44");
}

#[test]
fn tests() {
    assert_eq!(dinero_f32(3.1415926), "3,14");
    assert_eq!(dinero_f32(1000000.44), "1.000.000,43");
    assert_eq!(dinero_f64(10000000.44), "10.000.000,43");

    assert_eq!(
        endinero_f64(1234567.456, 2, 4, '.', ',', ' '),
        "1.234.567,45"
    );

    assert_eq!(
        endinero_f64(-1234567.456789, 2, 4, '.', ',', ' '),
        "-1.234.567,45"
    );

    assert_eq!(
        endinero_f64(-1234567.456789, // amount
                     4, // decimals for numbers > 0
                     4, // decimals for numbers < 0
                     '.', // thousands separator
                     ',', // radix separator
                     ' '), // decimals separator
        "-1.234.567,456 7"
    );

    assert_eq!(endinero_f64(0.456, 2, 4, '.', ',', ' '), "0,456");
    assert_eq!(endinero_f64(0.456789, 2, 4, '.', ',', '.'), "0,456.7");
    assert_eq!(endinero_f64(0.456789, 2, 5, '.', ',', '.'), "0,456.78");
    assert_eq!(endinero_f64(0.456789, 2, 5, '.', '🔻', '.'), "0🔻456.78");
    assert_eq!(endinero_f64(0.456789, 2, 6, '.', '🔻', '.'), "0🔻456.789");

    assert_eq!(dinero_f64(0.1234567), "0,123 456 7");
    assert_eq!(dinero_f64(10000000.1232456), "10.000.000,12");

    // info!("Hundreds of trillions (10^14) / Cientos de billones");
    let x: f64 = 123456789012345.1234;
    assert_eq!(dinero_f64(x), "123.456.789.012.345,12"); // rounding issue

    // should break here
    assert_ne!(dinero_f64(x), "1.234.567.890.123.456,12");
    assert_eq!(dinero_f64(0.12345678912345678), "0,123 456 789 123 456 78");

    assert_eq!(dinero_f32(10.111), "10,11");
    assert_eq!(dinero_f32(-10.111), "-10,11");


    assert_eq!(dinero_f32(0.2223334445556), "0,222 333 4");

    // rounding issue here
    assert_eq!(dinero_f32(11111111.1234), "11.111.111,0");
    assert_eq!(dinero_f32(0.1234567), "0,123 456 7");

    assert_eq!(money_f32(12345678.123456), "12,345,678.12");
    assert_eq!(money_f32(1.123456),"1.12");
    assert_eq!(money_f32(0.123456789),"0.123 456 7");
    assert_eq!(money_f64(0.123456789),"0.123 456 789");
    assert_eq!(money_f64(0.123456789012345),"0.123 456 789 012 345");
}