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

Round R Markdown numbers to 1 decimal place when >= 1 #24

Merged
merged 29 commits into from
May 19, 2021
Merged

Conversation

kelly-sovacool
Copy link
Member

@kelly-sovacool kelly-sovacool commented Apr 14, 2021

By request of @pschloss.

If a number is "nearly whole" (abs(x - round(x)) < .Machine$double.eps^0.5), it is rounded to zero decimal places.
Otherwise, it is rounded to contain two significant digits if it is less than 1, or rounded to one decimal place if greater than or equal to 1.

Examples:

> inline_hook(0.0256)
[1] "0.026"
> inline_hook(.Machine$double.eps^0.5)
[1] "0.000000015"
> inline_hook(100000.08)
[1] "100,000.1"
> inline_hook(1.00000000000000000001)
[1] "1"
> inline_hook("this is a string")
[1] "this is a string"

I also refactored the code so the function that used to be called inline_hook() is now named format_numbers() and a new inline_hook() function simply calls both format_numbers() and paste_oxford_list().

@kelly-sovacool kelly-sovacool requested review from a team and removed request for a team May 19, 2021 15:56
@kelly-sovacool kelly-sovacool changed the base branch from master to main May 19, 2021 15:59
@courtneyarmour
Copy link
Contributor

these functions are soooooo useful!

@courtneyarmour courtneyarmour merged commit 4b082be into main May 19, 2021
@kelly-sovacool kelly-sovacool deleted the digits branch May 20, 2021 21:50
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.

2 participants