#numbers #string #conversion #perform #separator #convert-string #culture

num_string

一个简单的库,用于在字符串和数字之间进行转换

1 个不稳定版本

0.1.0 2022年7月2日

#14 in #separator

MIT 许可协议

78KB
1.5K SLoC

在字符串和数字之间进行转换

made-with-rust Build Status License:MIT

允许将文化格式化的数字转换为 Rust 数字

允许将 Rust 数字显示为文化格式化的字符串

字符串转数字示例

基本字符串转数字

use num_string::{Culture, ConversionError, NumberConversion};
    assert_eq!("1000".to_number::<i32>().unwrap(), 1000);
    assert_eq!("+1000".to_number::<i64>().unwrap(), 1000);
    assert_eq!("-1000".to_number::<i64>().unwrap(), -1000);
    assert_eq!("1000".to_number::<f32>().unwrap(), 1000.0);
    assert_eq!("1000.5822".to_number::<f32>().unwrap(), 1000.5822);
    
    // Fail because 1000 > i8 max capacity
    assert_eq!("1000".to_number::<i8>(), Err(ConversionError::UnableToConvertStringToNumber));

对于更高级的转换,您可以指定文化

use num_string::{Culture, NumberConversion};     
    // Numbers with decimal separator
    assert_eq!("10.8888".to_number_culture::<f32>(Culture::English).unwrap(), 10.8888);
    assert_eq!("0,10".to_number_culture::<f32>(Culture::Italian).unwrap(), 0.1); 

    // Numbers with decimal separator and no whole part
    assert_eq!(",10".to_number_culture::<f32>(Culture::Italian).unwrap(), 0.1); 

    // Numbers with thousand separator
    assert_eq!("1,000".to_number_culture::<i32>(Culture::English).unwrap(), 1000);     

    // Numbers with thousand and decimal separator
    assert_eq!("1,000.8888".to_number_culture::<f32>(Culture::English).unwrap(), 1000.8888);
    assert_eq!("-10 564,10".to_number_culture::<f32>(Culture::French).unwrap(), -10564.10);

自定义分隔符(点作为千位分隔符,空格作为小数分隔符)

use num_string::{NumberCultureSettings, Separator, NumberConversion};

    assert_eq!(
            "1.000 8888"
                .to_number_separators::<f32>(NumberCultureSettings::new(
                    Separator::DOT,
                    Separator::SPACE
                ))
                .unwrap(),
            1000.8888
        );

数字转字符串示例

use num_string::{Culture, ToFormat}; 
    // Some basic display (N0 = 0 digit, N2 = 2 digits etc)
    assert_eq!(1000.to_format("N0", Culture::English).unwrap(), "1,000");
    assert_eq!((-1000).to_format("N0", Culture::English).unwrap(), "-1,000");
    assert_eq!(1000.to_format("N2", Culture::French).unwrap(), "1 000,00");

    // Perform the round decimal
    assert_eq!(10_000.9999.to_format("N2", Culture::French).unwrap(), "10 001,00");
    assert_eq!((-10_000.999).to_format("N2", Culture::French).unwrap(), "-10 001,00");

数字分析示例

use num_string::{ConvertString, Culture};
use num_string::pattern::TypeParsing; 
    let string_num = ConvertString::new("1,000.2", Some(Culture::English));
    assert!(string_num.is_numeric());
    assert!(string_num.is_float());
    assert!(!string_num.is_integer()); 

    // Convert to number
    assert_eq!(string_num.to_number::<f32>().unwrap(), 1000.2); 
    
    // If the conversion is ok (string_num.isNumeric() == true), you will have access to the matching pattern
    let matching_pattern = string_num.get_current_pattern().unwrap();
    assert_eq!(matching_pattern.get_regex().get_type_parsing(), &TypeParsing::DecimalThousandSeparator); 

    // If we try to convert a bad formatted number
    let string_error = ConvertString::new("NotANumber", Some(Culture::English));
    assert!(!string_error.is_numeric());

如有需要,请随时fork或联系我

依赖项

~2.6–3.5MB
~65K SLoC