Separate number and SI prefix by narrow non-breakable space

This looks better and more closely emulates the proper typesetting
of units. Bumped patch version number.
master
Taha Ahmed 10 months ago
parent 64c4b6004d
commit 816d05dced

@ -1,7 +1,7 @@
Package: common
Type: Package
Title: A collection of solarchemist's common R functions
Version: 0.1.0
Version: 0.1.1
Description: A package containing solarchemist's commonly-used R functions and scripts.
Authors@R: person("Taha", "Ahmed", email = "taha@chepec.se", role = c("aut", "cre"))
License: GPL-3 + file LICENSE

@ -60,7 +60,8 @@ is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) {
#' @param rounding rounds number to nearest integer (default FALSE), boolean
#' @param digits if rounding=FALSE, lets you specify significant figures (default 6), numeric
#'
#' @return number with SI prefix (as character string)
#' @return number followed by SI prefix (as character string,
#' separated by narrow no-break space)
#' @export
numbers2prefix <- function(number, rounding = FALSE, digits = ifelse(rounding, NA, 6)) {
# https://www.nist.gov/pml/owm/metric-si-prefixes
@ -96,12 +97,12 @@ numbers2prefix <- function(number, rounding = FALSE, digits = ifelse(rounding, N
ix <- findInterval(x = number, vec = lut$factor)
if (ix > 0 && ix < length(lut$factor) && lut$factor[ix] != 1) {
if (rounding == TRUE && !is.numeric(digits)) {
sistring <- paste(round(number / lut$factor[ix]), lut$symbol[ix])
sistring <- paste0(round(number / lut$factor[ix]), "\u202f", lut$symbol[ix])
} else
if (rounding == TRUE || is.numeric(digits)) {
sistring <- paste(signif(number / lut$factor[ix], digits), lut$symbol[ix])
sistring <- paste0(signif(number / lut$factor[ix], digits), "\u202f", lut$symbol[ix])
} else {
sistring <- paste(number / lut$factor[ix], lut$symbol[ix])
sistring <- paste0(number / lut$factor[ix], "\u202f", lut$symbol[ix])
}
} else {
sistring <- as.character(number)

@ -36,11 +36,11 @@ I suggest the following package rebuild procedure:
+ Run `devtools::check()`.
Should complete with no warnings, errors or notes:
```
── R CMD check results ─────────────────────────────────────── common 0.1.0 ────
── R CMD check results ─────────────────────────────────────── common 0.1.1 ────
Duration: 8.1s
0 errors ✔ | 0 warnings ✔ | 0 notes ✔
```
+ If necessary, run `devtools::document()` to update the documentation.
+ As necessary, run `devtools::document()` to update the documentation.
Contributions are welcome, no matter whether code, bug reports or suggestions!

@ -14,7 +14,8 @@ numbers2prefix(number, rounding = FALSE, digits = ifelse(rounding, NA, 6))
\item{digits}{if rounding=FALSE, lets you specify significant figures (default 6), numeric}
}
\value{
number with SI prefix (as character string)
number followed by SI prefix (as character string,
separated by narrow no-break space)
}
\description{
Using SI unit prefixes is a more compact way to write very large or very small

Loading…
Cancel
Save