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

feature request: limit precision of floats in pretty #112

Closed
2 tasks
ctb opened this issue Oct 18, 2020 · 2 comments
Closed
2 tasks

feature request: limit precision of floats in pretty #112

ctb opened this issue Oct 18, 2020 · 2 comments

Comments

@ctb
Copy link
Contributor

ctb commented Oct 18, 2020

Prerequisites

  • make sure you're are using the latest version by csvtk version
  • read the usage

Describe your issue

I have a CSV with floating point numbers in it, and it would be great to limit them to ~3 signif digits -- here's what it looks like currently. I can't find a flag to do that tho!

csvtk cut twofoo_k31_r1_search_oh0/results.csv  -f best_containment,cdbg_min_overhead,catlas_min_overhead | csvtk pretty
best_containment     cdbg_min_overhead        catlas_min_overhead
0.9999198549640733   1.4814505299984235e-05   0.005855691531263713
0.7734263551896505   0.07683380394531308      0.2544437040336933
0.9478159807721671   0.02152625515812585      0.17919530049961052
@shenwei356
Copy link
Owner

shenwei356 commented Oct 19, 2020

Added a new command round, v0.21.0rc1

You can use

csvtk cut twofoo_k31_r1_search_oh0/results.csv  -f best_containment,cdbg_min_overhead,catlas_min_overhead \
    | csvtk round -a \
    | csvtk pretty -r
best_containment   cdbg_min_overhead   catlas_min_overhead
           1.000           1.481e-05                 0.006
           0.773               0.077                 0.254
           0.948               0.022                 0.179

Usage:

round float to n decimal places

Usage:
  csvtk round [flags]

Flags:
  -a, --all-fields          all fields, overides -f/--fields
  -n, --decimal-width int   limit floats to N decimal points (default 2)
  -f, --fields string       select only these fields. e.g -f 1,2 or -f columnA,columnB (default "1")
  -F, --fuzzy-fields        using fuzzy fields, e.g., -F -f "*name" or -F -f "id123*"
  -h, --help                help for round

Examples:

$ cat testdata/floats.csv  | csvtk pretty
a                        b
0.12345                  abc
NA                       0.9999198549640733
12.3                     e3
1.4814505299984235e-05   -3.1415926E05

# one or more fields
$ cat testdata/floats.csv | csvtk round -n 2 -f b | csvtk pretty 
a                        b
0.12345                  abc
NA                       1.00
12.3                     e3
1.4814505299984235e-05   -3.14E05

# all fields
$ cat testdata/floats.csv | csvtk round -n 2 -a | csvtk pretty 
a          b
0.12       abc
NA         1.00
12.30      e3
1.48e-05   -3.14E05

@ctb
Copy link
Contributor Author

ctb commented Oct 20, 2020 via email

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

No branches or pull requests

2 participants