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().
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:
I also refactored the code so the function that used to be called
inline_hook()
is now namedformat_numbers()
and a newinline_hook()
function simply calls bothformat_numbers()
andpaste_oxford_list()
.