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

Scaled values in fmt_scientific() can result in x 10^0-type output #229

Closed
rich-iannone opened this issue Mar 20, 2019 · 0 comments · Fixed by #232
Closed

Scaled values in fmt_scientific() can result in x 10^0-type output #229

rich-iannone opened this issue Mar 20, 2019 · 0 comments · Fixed by #232

Comments

@rich-iannone
Copy link
Member

The fmt_scientific() formatter is designed to internally label numeric values that would lead to power-of-zero figures (e.g., 1.53 x 10^0 should just be 1.53). This results in the exclusion of those labeled numbers in getting a x 10^y part (which would end up being x 10^0).

However, the current method can result in incorrect formatting when values are scaled with scale_by != 1. Some scaled numbers incorrectly get formatted with a x 10^0 and others don't get a required x 10^y part. Here is an example:

dplyr::tibble(
  unscaled_num = c(0.1, 0.5, 1.5, 7.5, 10.5),
  unscaled_sci = c(0.1, 0.5, 1.5, 7.5, 10.5),
  scaled_num = c(0.1, 0.5, 1.5, 7.5, 10.5),
  scaled_sci = c(0.1, 0.5, 1.5, 7.5, 10.5)
) %>%
  gt() %>%
  fmt_number(
    columns = vars(unscaled_num)
  ) %>%
  fmt_scientific(
    columns = vars(unscaled_sci),
  ) %>%
  fmt_number(
    columns = vars(scaled_num),
    scale_by = 3
  ) %>%
  fmt_scientific(
    columns = vars(scaled_sci),
    scale_by = 3
  )

fmt_scientific_scaling

where rows 2 and 4 are incorrectly formatted. The solution is to scale the incoming values first, and then perform the checks for excluding the x 10^0 part.

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

Successfully merging a pull request may close this issue.

1 participant