Comments (4)
You want the normalize
[1] or normalize_assign
[2] functions, to strip the extra 0.
[1] https://docs.rs/rust_decimal/latest/rust_decimal/struct.Decimal.html#method.normalize
[2] https://docs.rs/rust_decimal/latest/rust_decimal/struct.Decimal.html#method.normalize_assign
from rust-decimal.
Ah awesome!
Should the docs of set_scale
maybe be adjusted so that future users know about it?
from rust-decimal.
Thanks @Tony-Samuels for your reply - exactly right.
@Zarathustra2 - I think there is always an opportunity to make the documentation clearer. In this case, would it help to describe what set_scale
is doing under the hood? i.e. we maintain the same mantissa, but just change where the decimal point is - hence retaining precision without rounding. e.g. in this example 3850 is having the decimal point moved to the left 3 places.
from rust-decimal.
@paupino Yes some clearer docs would be nice. Maybe we can also add a code example directly to the docs as example. I personally think a coding example with comments is more clear than plain text.
let num = 3850;
let dec = Decimal::from(num) / Decimal::ONE_THOUSAND;
let mut dec_scale = Decimal::from(num);
dec_scale.set_scale(3).unwrap();
// Passes since same number
assert_eq!(dec, dec_scale);
assert_eq!(dec.to_string(), "3.85".to_string());
// set_scale just moves the decimal point and keeps original mantisse.
// this effects the string representation but both dec & dec_scale represent
// the same number
assert_eq!(dec_scale.to_string(), "3.850".to_string());
assert_eq!(dec_scale, dec);
assert_ne!(dec_scale.to_string(), dec.to_string());
// Use normalize to normalize the mantisse
assert_eq!(dec_scale.normalize().to_string(), "3.85".to_string());
from rust-decimal.
Related Issues (20)
- How to serialize Decimal and still keep the results in order (bitwise comparison) HOT 3
- Serialization V2 - brainstorming HOT 1
- feature request: JSON schema support via https://github.com/GREsau/schemars HOT 1
- Feature Request: Support wasm_bindgen for Decimal
- rlp (de)serialization? HOT 2
- Rust Decimal: Request for additional repository Owner/Collaborator(s) HOT 1
- Rescaling a value with significant scale 28 to scale 29 creates a value with scale 29 and overflows on subtraction HOT 1
- Support for mongo HOT 2
- To f64 losing accuracy for `100000.000000000000000000` HOT 1
- too rubbish to called rust decimal HOT 3
- checked_powd takes exponentially long time to complete HOT 6
- Scale changes unexpectedly after operations HOT 3
- 1.34.0 breaks with cyclic dependency when installed with "macros" feature HOT 4
- Deserializing `Decimal`s from Postgres using the `db-postgres` feature can lead to invalid `Decimal` instances HOT 4
- Bug: not 128bit aligned with `repr-c` feature HOT 3
- Return error rather than panic in `FromSql` when overflow HOT 1
- Bug: Decimal::from_sql incorrectly transforms Postgres NaN to 0 HOT 1
- rust_decimal_macros 1.35.0 not on crates.io (Or: Error in Documentation) HOT 2
- Diesel1 feature is breaking the build
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-decimal.