Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass formatter parameters to WKT writer #200

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kylebarron
Copy link
Member

This is just a prototype of #199.

Based on the example here, this optionally passes down a precision parameter to the format!() call.

One thing I noticed when I hard-coded format!("{x:.3} {y:.3}") is that setting that forces even integers to have 3 decimal points. E.g.

---- geojson::geojson_reader::test::conversions stdout ----
thread 'geojson::geojson_reader::test::conversions' panicked at geozero/src/geojson/geojson_reader.rs:419:9:
assertion `left == right` failed
  left: "POINT(10.000 20.000)"
 right: "POINT(10 20)"

Potentially this means what I actually want to do to solve my goal in #199 is to truncate the precision of my data, and that will automatically get printed as I want...

@michaelkirk
Copy link
Member

truncate the precision of my data, and that will automatically get printed as I want...

I think that approach will work (1.0 - 0.07) == 0.9299999999999999... percent of the time. 😉

@michaelkirk
Copy link
Member

Dumb jokes aside, you mean something like this, right?

let y = (x * 10.pow(n)).round() / 10.pow(n);
format!("{}", y);

I think that should work 100 percent of the time, though opens us up to loss of precision at high values.

I don't know why, but I'm sort of surprised that a formatting option like this doesn't already exist.

@kylebarron
Copy link
Member Author

Dumb jokes aside, you mean something like this, right?

let y = (x * 10.pow(n)).round() / 10.pow(n);
format!("{}", y);

Yeah. That's how I ended up implementing it in geoarrow: https://github.com/geoarrow/geoarrow-rs/blob/667389b2a1cdae704b815b6a447505027d876ff9/src/io/display/scalar.rs#L18-L21

opens us up to loss of precision at high values.

Hmm yeah that's true. I'm not sure how to truncate floats with high precision.

I don't know why, but I'm sort of surprised that a formatting option like this doesn't already exist.

You mean in core rust? Maybe it does; I haven't searched for it.

@frewsxcv
Copy link
Member

frewsxcv commented Oct 2, 2024

fn main() {
    let num = 3.14159;
    let decimals = 3;
    let formatted = format!("{:.1$}", num, decimals);
    println!("{}", formatted);  // Output: 3.142
}

@kylebarron
Copy link
Member Author

@frewsxcv that's what I'm doing here:
https://github.com/georust/geozero/pull/200/files#diff-553c2186acf21c8c360979841037ea9343c11e4e2937438cb9eca2c20d0d08cdR107-R109

But we'd still need a way for the user to pass down the precision desired

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants