From 344bb81054a79aa7246f7f4292e94210d0d679dd Mon Sep 17 00:00:00 2001 From: Wei Shen Date: Mon, 11 Jan 2021 18:48:00 +0800 Subject: [PATCH] csvtk round: fix bug of failing to round scientific notation with value small than one --- CHANGES.md | 1 + csvtk/cmd/round.go | 4 ++++ doc/docs/download.md | 1 + 3 files changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index f153edb..86deb89 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ - new command: `csvtk unfold` for unfolding multiple values in cells of a field. [#103](https://github.com/shenwei356/csvtk/issues/103) - rename `csvtk collapse` to `csvtk fold`, for folding multiple values of a field into cells of groups. - `csvtk cut`: **support range format `2-` to choose 2nd column to the end**. [#106](https://github.com/shenwei356/csvtk/issues/106) + - `csvtk round`: fix bug of failing to round scientific notation with value small than one, e.g., `7.1E-1`. - [csvtk v0.21.0](https://github.com/shenwei356/csvtk/releases/tag/v0.21.0) [![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.21.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.21.0) - new command: `csvtk nrow/ncol` for printing number of rows or columns. diff --git a/csvtk/cmd/round.go b/csvtk/cmd/round.go index f9b501f..f25dad5 100644 --- a/csvtk/cmd/round.go +++ b/csvtk/cmd/round.go @@ -26,6 +26,7 @@ import ( "regexp" "runtime" "strconv" + "strings" "github.com/shenwei356/xopen" "github.com/spf13/cobra" @@ -220,6 +221,9 @@ var roundCmd = &cobra.Command{ record2[f] = fmt.Sprintf(decimalFormat, fvalue) } else if found[1] == "" { + } else if strings.IndexByte(found[2], '-') > 0 { // scientific notation with value < 1 + fvalue, _ = strconv.ParseFloat(record2[f], 10) + record2[f] = fmt.Sprintf(decimalFormat, fvalue) } else { fvalue, _ = strconv.ParseFloat(found[1], 10) record2[f] = fmt.Sprintf(decimalFormat, fvalue) + found[2] diff --git a/doc/docs/download.md b/doc/docs/download.md index 1bf4628..7f1c4b7 100644 --- a/doc/docs/download.md +++ b/doc/docs/download.md @@ -15,6 +15,7 @@ - new command: `csvtk unfold` for unfolding multiple values in cells of a field. [#103](https://github.com/shenwei356/csvtk/issues/103) - rename `csvtk collapse` to `csvtk fold`, for folding multiple values of a field into cells of groups. - `csvtk cut`: **support range format `2-` to choose 2nd column to the end**. [#106](https://github.com/shenwei356/csvtk/issues/106) + - `csvtk round`: fix bug of failing to round scientific notation with value small than one, e.g., `7.1E-1`. Links: