numbers2swedish() converts numbers into their spelled words in Swedish

Note that this is just a quick-and-dirty implementation, and the returned Swedish string is not grammatically correct in all cases.
master
Taha Ahmed 4 years ago
parent 77ae28f0d4
commit 38db263a39

@ -1,13 +1,13 @@
Package: common
Type: Package
Title: chepec common
Version: 0.0.1.9000
Version: 0.0.1.9001
Description: Commonly used functions and scripts.
Authors@R: person("Taha", "Ahmed", email = "taha@chepec.se", role = c("aut", "cre"))
License: GPL-3
Encoding: UTF-8
LazyData: TRUE
RoxygenNote: 6.1.1
RoxygenNote: 7.1.1
Imports:
stats,
knitr,

@ -15,6 +15,7 @@ export(as.radians)
export(int2padstr)
export(is.wholenumber)
export(molarity2mass)
export(numbers2swedish)
export(numbers2words)
export(pascal2torr)
export(roundup)

@ -113,3 +113,61 @@ numbers2words <- function(x, billion = c("US", "UK"), and = if (billion == "US")
x <- round(x)
if (length(x) > 1) sapply(x, helper) else helper(x)
}
#' numbers2swedish
#'
#' Converts a number into its corresponding words in Swedish
#' This is my own adaptation of numbers2words()
#' I think an even neater solution would be to have an argument
#' numbers2words(lang = "swe"), but that would require more coding
#' THIS FUNCTION NEEDS WORK
#' Cannot return proper grammar: "en miljon" and "ett tusen", or
#' "en miljon" and "två miljoner", and so on.
#'
#' @param x number
#'
#' @return string
#' @export
numbers2swedish <- function(x) {
and <- ""
trim <- function(text) {
gsub("(^\ *)|((\ *|-|,\ noll|-noll)$)", "", text)
}
makeNumber <- function(x) as.numeric(paste(x, collapse = ""))
makeDigits <- function(x) strsplit(as.character(x), "")[[1]]
helper <- function(x) {
negative <- x < 0
x <- abs(x)
digits <- makeDigits(x)
nDigits <- length(digits)
result <- if (nDigits == 1) as.vector(ones[digits])
else if (nDigits == 2)
if (x <= 19) as.vector(teens[digits[2]])
else trim(paste(tens[digits[1]], "-", ones[digits[2]], sep=""))
else if (nDigits == 3) {
tail <- makeNumber(digits[2:3])
if (tail == 0) paste(ones[digits[1]], "hundra")
else trim(paste(ones[digits[1]], trim(paste("hundra", and)),
helper(tail)))
} else {
nSuffix <- ((nDigits + 2) %/% 3) - 1
if (nSuffix > length(suffixes) || nDigits > 15)
stop(paste(x, "is too large!"))
pick <- 1:(nDigits - 3 * nSuffix)
trim(paste(helper(makeNumber(digits[pick])), suffixes[nSuffix], helper(makeNumber(digits[-pick]))))
}
if (negative) paste("minus", result) else result
}
opts <- options(scipen = 100)
on.exit(options(opts))
ones <- c("noll", "ett", "tv\U00E5", "tre", "fyra", "fem", "sex", "sju", "\U00E5tta", "nio")
teens <- c("tio", "elva", "tolv", "tretton", "fjorton", "femton", "sexton", "sjutton", "arton", "nitton")
names(ones) <- names(teens) <- 0:9
tens <- c("tjugo", "trettio", "fyrtio", "femtio", "sextio", "sjuttio", "\U00E5ttio", "nittio")
names(tens) <- 2:9
# https://sv.wikipedia.org/wiki/Namn_p%C3%A5_stora_tal
suffixes <- c("tusen,", "miljoner,", "miljarder,", "biljoner,")
x <- round(x)
if (length(x) > 1) sapply(x, helper) else helper(x)
}

@ -2,9 +2,11 @@
Includes common numerical functions and some LaTeX-specific functions.
Note that the package is named `common`, despite the repository having a different name.
Note that the package is named `common`, despite the repository being name `R-common`.
Sorry about that (this was one of the very first packages I created, and I haven't
gotten around to fixing the naming mismatch yet).
[This repository is mirrored on my Gitea](https://git.chepec.se/taha/common).
[This repository is mirrored on my Gitea](https://git.solarchemist.se/taha/common).

@ -4,9 +4,15 @@
\alias{GenericXtableSetAttributes}
\title{Set the attributes for a generic xtable object}
\usage{
GenericXtableSetAttributes(xtobject, nxtnames = NULL, nxtdigits = NULL,
nxtdisplay = NULL, nxtalign = NULL, caption.text = "nxtcaption",
caption.label = "tab:nxtlabel")
GenericXtableSetAttributes(
xtobject,
nxtnames = NULL,
nxtdigits = NULL,
nxtdisplay = NULL,
nxtalign = NULL,
caption.text = "nxtcaption",
caption.label = "tab:nxtlabel"
)
}
\arguments{
\item{xtobject}{the xtable (table)}

@ -4,8 +4,14 @@
\alias{SubfigureGenerator}
\title{Generate LaTeX subfigure code}
\usage{
SubfigureGenerator(images, subcaptions, mainlabel = "fig:mainfig",
perpage = 6, ncol = 2, landscape = FALSE)
SubfigureGenerator(
images,
subcaptions,
mainlabel = "fig:mainfig",
perpage = 6,
ncol = 2,
landscape = FALSE
)
}
\arguments{
\item{images}{vector with full paths to images (png-files or other LaTeX-compatible format)

@ -0,0 +1,23 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/common.R
\name{numbers2swedish}
\alias{numbers2swedish}
\title{numbers2swedish}
\usage{
numbers2swedish(x)
}
\arguments{
\item{x}{number}
}
\value{
string
}
\description{
Converts a number into its corresponding words in Swedish
This is my own adaptation of numbers2words()
I think an even neater solution would be to have an argument
numbers2words(lang = "swe"), but that would require more coding
THIS FUNCTION NEEDS WORK
Cannot return proper grammar: "en miljon" and "ett tusen", or
"en miljon" and "två miljoner", and so on.
}

@ -4,8 +4,11 @@
\alias{numbers2words}
\title{numbers2words}
\usage{
numbers2words(x, billion = c("US", "UK"), and = if (billion == "US") ""
else "and")
numbers2words(
x,
billion = c("US", "UK"),
and = if (billion == "US") "" else "and"
)
}
\arguments{
\item{x}{number}

Loading…
Cancel
Save