Integer to string based on the language

Hey everyone, I have two functions
fn parse_as_integer(input: &str) -> Result<i32, String>
that contains .is_numeric() and .parse::<i32>()
for (i, y) in input.char_indices() { if !y.is_numeric() …
if let Ok(number) = input[...position].parse::<i32>() …

And the function
fn format_fraction(num: i32) -> &str
that contains .to_string()

From the Rust Docs - char the is_numeric() should be able to detect numbers in different languages. My questions are,

Can .parse::<i32>() convert number strings in different languages?

How would I use .to_string() (or another method/s) to convert numbers to a string based on the users language?

p.s. I am using GTK-rs and related libraries for Internationalization and Localization.

p.s.s. I haven't created any tests for these two functions, yet.

I think "numbers" are assumed to be strings of '0123456789'. Not other codepoints used by other languages.

While is_numeric() relies on Unicode properties, the FromStr implementations (and thus parse()) use the same logic as from_str_radix(_) and to_digit(_) -- ASCII digits and letters (depending on the radix) only.

You're definitely going to need a library (other than the standard library). Out of curiosity I did some searching and found the ICU RBNF, which can apparently parse as well as display numbers textually. Live examples seem sparse though. I think in general it's a tall order, but maybe someone else knows of a different library.

Bummer, but thanks.
I was hoping the standard library would do the conversion for me.
The first 3 asserts below are true but the last fails with ParseIntError { kind: InvalidDigit }

assert_eq!(420, "٤٢٠".parse::().unwrap());

@quinedot yeah, for the to_string() I don't expect the standard library to handle multiple languages. But hopefully the standard library could handle more unicode numbers within their other functions.

I might see if GTK has something and if gtk-rs has a rust function for it.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.