3 ms·
How do you store numbers with leading zeros?\
- geophile 5y agoThe question conflates representation and presentation. To be concrete, let's suppose we're talking about 5-digit, zero-padded numbers. Those are integers in the range [0, 99999]. Any database system will allow you to model this value and enforce the constraint in the DDL. Storage is compact, the constraint is system-enforced, and querying works correctly, i.e., you get the right result whether your query bounds are zero-padded or not (with some assumptions about permitted literal syntax). The zero-padding is only relevant for presentation, which should not be mixed up with the modeling in any case. Also, this assumes fixed-length. If, for example, 5, 05, 005, 0005 and 00005 are all valid and different, then just use a string.
- metaloha 5y agoNo point in over-complicating things. If serial `00001` and `1` are meant to be different, then store it as a string. If they would definitely refer to the same item, strip the leading zeroes for storage (and comparisons!) and keep it as a string. I go with string because serial numbers, despite having the word "number" in the phrase, are not necessarily numeric. Hexadecimal numbers have letters (for example), but we still call them "numbers" ;)
- Cryptonic 5y agoDepends very much on what exactly you need. Up to 2^64-1 leading zeros followed by as high as you like (the RAM is the limit) positive value? struct NumberWithLeadingZeros { leading_zeros: u64, number: Vec<u64> } Then implement some traits for it add, sub, pow, to_string etc. and work with it. Or do something else that fits exactly your needs.