r2B: Conversion of real to string and rounding, in given base

Description Usage Arguments Value Note Author(s) Examples

Description

Functions for conversion of real to string and back, in given base, in fixed and exponential format; and rounding in base number system.

Usage

1
2
3
4
5
6
7
  r2B(x, base = 10, rnd = 0, space = 0, plus = "", lead = "", 
    just = c("right","left","center","none") )
  r2Be(x, base = 10, space = 4,plus = "+",
    just = c("right","left","center","none") )
  roundB( x, base=10, rnd=0 )
  strB2r( STR, base=10 )
  strB2i( STR, base=10 )

Arguments

x

Real or integer, vector

STR

Vector of strings representing reals in a given base

base

2 <= integer <= 60, base of representation

space

Integer, space for resulting string;
if too small, only necessary space will be taken. All components of the result will be of common length, justified according to 'just'.

rnd

Integer, number of places (after ".") to be rounded;
= 0: rounded integer, no decimal point;
= 0.5: rounded integer "." no following digits shown;
< 0: rounding 'rnd' places *before* last integer digit;
If too negative, 0 will result; see examples;
'rnd' prevalent over 'space', i.e. space will be expanded if necessary.

plus

use "+" to show sign for positive values.

lead

use "" or " "; or "0" for leading zeros; this will be inserted after sign.

just

Choice of insertion of justification, can be abbreviated, see justify

Value

roundB: vector of arguments rounded to rnd places according to base base representation

r2B: list( s=vector of strings represention of x, rounded to rnd decimal digits, base=base)

r2Be: like r2B, but in exponential representation, if space too small. The exponent marker is "e" for base==10 and EXPCHAR otherwise. strB2r: real corresponding to string representation. NaN is returned if str contains characters not in HexagesDig[1:base] (as are generated by r2B, r2Be ).

strB2i: integer ( str2i ) corresponding to string representation, used in strB2r

Note

r2B(.) and strB2(.) are inverses of each other.

r2Be chooses between fixed and exponential format depending on available space, adjusting rounding accordingly. just="left" works best with lead = "" = default.

strB2r can convert strings with exponent signifyers "e" (for decimals) and "z" for others, "." is allowed for fractional parts.

strB2i works on strings without "e","z","." ONLY!

Author(s)

Christian W. Hoffmann <christian@echoffmann.ch>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  x <- c(0,-0.0012345, 1.5234, 543, 8123456,NA,Inf,-Inf,NaN,1,pi)
  y <- c( 0, 1, pi*10^c(-27,-8,0, 8) )
# "+3.1e27" "    NA " "   NaN " "   Inf   )
r2B(x,,space=10,lead=" ",plus="",rnd=3)$s
# "          0 " "-      0.001" "       1.523" "     543.000"
# " 8123456.000" "          NA" "         Inf" "        -Inf"
# "         NaN" "       1.000" "       3.142"
  r2B(x,16,space=10,lead=" ")$s
# "        0 " "-        0" "         2" "       21F"
# "    7BF440" "        NA" "       Inf""      -Inf"
# "       NaN" "         1" "         3"
  r2B( x, 60, 4 )$s
  r2Be( y,10,7)$s
# "     0 " "+1.0000" "+3.e-27" "+3.1e-8" "+3.1416" "+3.14e8"
# "+3.1e27" "    NA " "   NaN " "   Inf "
  strB2r("-    9167.8  4",10)
  strB2r("B00z3",15 )  # 8353125
  (ii <- r2B( 8353125, 32, 4 )$s) # "7UTB5.0000"
  strB2r( ii, 32)  # 8353125
  roundB(c(0.4,0.3),2,16) # 0.39999390 0.30000305

cwhmisc documentation built on May 1, 2019, 7:55 p.m.