currency_usage/
currency_usage.rs

1use validator_rs::currency::{is_currency, CurrencyOptions};
2
3fn main() {
4    println!("=== Currency Validator Examples ===\n");
5
6    // Example 1: Default USD format
7    println!("1. Default USD format:");
8    let usd_values = vec!["$10,123.45", "10,123.45", "-$99.99", "$.99"];
9    for val in usd_values {
10        println!("   '{}' -> {}", val, is_currency(val, None));
11    }
12
13    // Example 2: Euro (Italian format)
14    println!("\n2. Euro (Italian format - €1.234,56):");
15    let euro_options = CurrencyOptions::new()
16        .symbol("€")
17        .thousands_separator('.')
18        .decimal_separator(',')
19        .allow_space_after_symbol(true);
20    
21    let euro_values = vec!["€1.234,56", "€ 1.234,56", "-€10,50"];
22    for val in euro_values {
23        println!("   '{}' -> {}", val, is_currency(val, Some(euro_options.clone())));
24    }
25
26    // Example 3: Chinese Yuan
27    println!("\n3. Chinese Yuan (¥):");
28    let yuan_options = CurrencyOptions::new()
29        .symbol("¥")
30        .negative_sign_before_digits(true);
31    
32    let yuan_values = vec!["¥1,234.56", "¥-999.99", "123,456.78"];
33    for val in yuan_values {
34        println!("   '{}' -> {}", val, is_currency(val, Some(yuan_options.clone())));
35    }
36
37    // Example 4: South African Rand
38    println!("\n4. South African Rand (R 123 or R-123):");
39    let rand_options = CurrencyOptions::new()
40        .symbol("R")
41        .thousands_separator(' ')
42        .decimal_separator(',')
43        .negative_sign_before_digits(true)
44        .allow_negative_sign_placeholder(true);
45    
46    let rand_values = vec!["R 10 123,45", "R-10 123,45", "R 123,45"];
47    for val in rand_values {
48        println!("   '{}' -> {}", val, is_currency(val, Some(rand_options.clone())));
49    }
50
51    // Example 5: Brazilian Real
52    println!("\n5. Brazilian Real (R$ 1.234,56):");
53    let real_options = CurrencyOptions::new()
54        .symbol("R$")
55        .require_symbol(true)
56        .allow_space_after_symbol(true)
57        .thousands_separator('.')
58        .decimal_separator(',');
59    
60    let real_values = vec!["R$ 1.400,00", "R$ 400,00", "$ 1.400,00"];
61    for val in real_values {
62        println!("   '{}' -> {}", val, is_currency(val, Some(real_options.clone())));
63    }
64
65    // Example 6: Parentheses for negatives
66    println!("\n6. Parentheses for negatives:");
67    let parens_options = CurrencyOptions::new()
68        .parens_for_negatives(true);
69    
70    let parens_values = vec!["($1,234.56)", "$1,234.56", "(1,234.56)", "-$1,234.56"];
71    for val in parens_values {
72        println!("   '{}' -> {}", val, is_currency(val, Some(parens_options.clone())));
73    }
74
75    // Example 7: Custom decimal digits
76    println!("\n7. Custom decimal digits (1 or 3 digits):");
77    let custom_decimal_options = CurrencyOptions::new()
78        .digits_after_decimal(vec![1, 3]);
79    
80    let custom_values = vec!["$10.5", "$10.123", "$10.12", "$10.1234"];
81    for val in custom_values {
82        println!("   '{}' -> {}", val, is_currency(val, Some(custom_decimal_options.clone())));
83    }
84
85    println!("\n=== All examples completed ===");
86}
87