Examples for 'base::formatC'


Formatting Using C-style Formats

Aliases: formatC prettyNum .format.zeros

Keywords: character print

### ** Examples

xx <- pi * 10^(-5:4)
cbind(format(xx, digits = 4), formatC(xx))
      [,1]        [,2]       
 [1,] "3.142e-05" "3.142e-05"
 [2,] "3.142e-04" "0.0003142"
 [3,] "3.142e-03" "0.003142" 
 [4,] "3.142e-02" "0.03142"  
 [5,] "3.142e-01" "0.3142"   
 [6,] "3.142e+00" "3.142"    
 [7,] "3.142e+01" "31.42"    
 [8,] "3.142e+02" "314.2"    
 [9,] "3.142e+03" "3142"     
[10,] "3.142e+04" "3.142e+04"
cbind(formatC(xx, width = 9, flag = "-"))
      [,1]       
 [1,] "3.142e-05"
 [2,] "0.0003142"
 [3,] "0.003142 "
 [4,] "0.03142  "
 [5,] "0.3142   "
 [6,] "3.142    "
 [7,] "31.42    "
 [8,] "314.2    "
 [9,] "3142     "
[10,] "3.142e+04"
cbind(formatC(xx, digits = 5, width = 8, format = "f", flag = "0"))
      [,1]         
 [1,] "00.00003"   
 [2,] "00.00031"   
 [3,] "00.00314"   
 [4,] "00.03142"   
 [5,] "00.31416"   
 [6,] "03.14159"   
 [7,] "31.41593"   
 [8,] "314.15927"  
 [9,] "3141.59265" 
[10,] "31415.92654"
cbind(format(xx, digits = 4), formatC(xx, digits = 4, format = "fg"))
      [,1]        [,2]        
 [1,] "3.142e-05" "0.00003142"
 [2,] "3.142e-04" "0.0003142" 
 [3,] "3.142e-03" "0.003142"  
 [4,] "3.142e-02" "0.03142"   
 [5,] "3.142e-01" "0.3142"    
 [6,] "3.142e+00" "3.142"     
 [7,] "3.142e+01" "31.42"     
 [8,] "3.142e+02" "314.2"     
 [9,] "3.142e+03" " 3142"     
[10,] "3.142e+04" "31416"     
f <- (-2:4); f <- f*16^f
# Default ("g") format:
formatC(pi*f)
[1] "-0.02454"  "-0.1963"   "0"         "50.27"     "1608"      "3.86e+04" 
[7] "8.235e+05"
# Fixed ("f") format, more than one flag ('width' partly "enlarged"):
cbind(formatC(pi*f, digits = 3, width=9, format = "f", flag = "0+"))
     [,1]         
[1,] "-0000.025"  
[2,] "-0000.196"  
[3,] "+0000.000"  
[4,] "+0050.265"  
[5,] "+1608.495"  
[6,] "+38603.891" 
[7,] "+823549.665"
formatC(    c("a", "Abc", "no way"), width = -7)  # <=> flag = "-"
[1] "a      " "Abc    " "no way "
formatC(c((-1:1)/0,c(1,100)*pi), width = 8, digits = 1)
[1] "    -Inf" "     NaN" "     Inf" "       3" "   3e+02"
## note that some of the results here depend on the implementation
## of long-double arithmetic, which is platform-specific.
xx <- c(1e-12,-3.98765e-10,1.45645e-69,1e-70,pi*1e37,3.44e4)
##       1        2             3        4      5       6
formatC(xx)
[1] "1e-12"      "-3.988e-10" "1.456e-69"  "1e-70"      "3.142e+37" 
[6] "3.44e+04"  
formatC(xx, format = "fg")       # special "fixed" format.
[1] "0.000000000001"                                                            
[2] "-0.0000000003988"                                                          
[3] "0.000000000000000000000000000000000000000000000000000000000000000000001456"
[4] "0.0000000000000000000000000000000000000000000000000000000000000000000001"  
[5] "31415926535897927981986333033020522496"                                    
[6] "34400"                                                                     
formatC(xx[1:4], format = "f", digits = 75) #>> even longer strings
[1] "0.000000000000999999999999999979886647629255615367252843506129522666014963761" 
[2] "-0.000000000398765000000000018320637518040800220675556886362755903974175453186"
[3] "0.000000000000000000000000000000000000000000000000000000000000000000001456450" 
[4] "0.000000000000000000000000000000000000000000000000000000000000000000000100000" 
formatC(c(3.24, 2.3e-6), format = "f", digits = 11)
[1] "3.24000000000" "0.00000230000"
formatC(c(3.24, 2.3e-6), format = "f", digits = 11, drop0trailing = TRUE)
[1] "3.24"      "0.0000023"
r <- c("76491283764.97430", "29.12345678901", "-7.1234", "-100.1","1123")
## American:
prettyNum(r, big.mark = ",")
[1] "76,491,283,764.97430" "      29.12345678901" "             -7.1234"
[4] "              -100.1" "               1,123"
## Some Europeans:
prettyNum(r, big.mark = "'", decimal.mark = ",")
[1] "76'491'283'764.97'430" "    29.12'345'678'901" "             -7.1'234"
[4] "               -100.1" "                1'123"
(dd <- sapply(1:10, function(i) paste((9:0)[1:i], collapse = "")))
 [1] "9"          "98"         "987"        "9876"       "98765"     
 [6] "987654"     "9876543"    "98765432"   "987654321"  "9876543210"
prettyNum(dd, big.mark = "'")
 [1] "            9" "           98" "          987" "        9'876"
 [5] "       98'765" "      987'654" "    9'876'543" "   98'765'432"
 [9] "  987'654'321" "9'876'543'210"
## examples of 'small.mark'
pN <- stats::pnorm(1:7, lower.tail = FALSE)
cbind(format (pN, small.mark = " ", digits = 15))
     [,1]                    
[1,] "1.58655 25393 1457e-01"
[2,] "2.27501 31948 1792e-02"
[3,] "1.34989 80316 3009e-03"
[4,] "3.16712 41833 1199e-05"
[5,] "2.86651 57187 9194e-07"
[6,] "9.86587 64503 7698e-10"
[7,] "1.27981 25438 8584e-12"
cbind(formatC(pN, small.mark = " ", digits = 17, format = "f"))
     [,1]                    
[1,] "0.15865 52539 31457 05"
[2,] "0.02275 01319 48179 21"
[3,] "0.00134 98980 31630 09"
[4,] "0.00003 16712 41833 12"
[5,] "0.00000 02866 51571 88"
[6,] "0.00000 00009 86587 65"
[7,] "0.00000 00000 01279 81"
cbind(ff <- format(1.2345 + 10^(0:5), width = 11, big.mark = "'"))
     [,1]          
[1,] "      2.2345"
[2,] "     11.2345"
[3,] "    101.2345"
[4,] "  1'001.2345"
[5,] " 10'001.2345"
[6,] "100'001.2345"
## all with same width (one more than the specified minimum)

## individual formatting to common width:
fc <- formatC(1.234 + 10^(0:8), format = "fg", width = 11, big.mark = "'")
cbind(fc)
      fc           
 [1,] "      2.234"
 [2,] "      11.23"
 [3,] "      101.2"
 [4,] "      1'001"
 [5,] "     10'001"
 [6,] "    100'001"
 [7,] "  1'000'001"
 [8,] " 10'000'001"
 [9,] "100'000'001"
## Powers of two, stored exactly, formatted individually:
pow.2 <- formatC(2^-(1:32), digits = 24, width = 1, format = "fg")
## nicely printed (the last line showing 5^32 exactly):
noquote(cbind(pow.2))
      pow.2                             
 [1,] 0.5                               
 [2,] 0.25                              
 [3,] 0.125                             
 [4,] 0.0625                            
 [5,] 0.03125                           
 [6,] 0.015625                          
 [7,] 0.0078125                         
 [8,] 0.00390625                        
 [9,] 0.001953125                       
[10,] 0.0009765625                      
[11,] 0.00048828125                     
[12,] 0.000244140625                    
[13,] 0.0001220703125                   
[14,] 0.00006103515625                  
[15,] 0.000030517578125                 
[16,] 0.0000152587890625                
[17,] 0.00000762939453125               
[18,] 0.000003814697265625              
[19,] 0.0000019073486328125             
[20,] 0.00000095367431640625            
[21,] 0.000000476837158203125           
[22,] 0.0000002384185791015625          
[23,] 0.00000011920928955078125         
[24,] 0.000000059604644775390625        
[25,] 0.0000000298023223876953125       
[26,] 0.00000001490116119384765625      
[27,] 0.000000007450580596923828125     
[28,] 0.0000000037252902984619140625    
[29,] 0.00000000186264514923095703125   
[30,] 0.000000000931322574615478515625  
[31,] 0.0000000004656612873077392578125 
[32,] 0.00000000023283064365386962890625
## complex numbers:
r <- 10.0000001; rv <- (r/10)^(1:10)
(zv <- (rv + 1i*rv))
 [1] 1+1i 1+1i 1+1i 1+1i 1+1i 1+1i 1+1i 1+1i 1+1i 1+1i
op <- options(digits = 7) ## (system default)
(pnv <- prettyNum(zv))
 [1] "1+1i" "1+1i" "1+1i" "1+1i" "1+1i" "1+1i" "1+1i" "1+1i" "1+1i" "1+1i"
stopifnot(pnv == "1+1i", pnv == format(zv),
          pnv == prettyNum(zv, drop0trailing = TRUE))
## more digits change the picture:
options(digits = 8)
head(fv <- format(zv), 3)
[1] "1.0000000+1.0000000i" "1.0000000+1.0000000i" "1.0000000+1.0000000i"
prettyNum(fv)
 [1] "1.0000000+1.0000000i" "1.0000000+1.0000000i" "1.0000000+1.0000000i"
 [4] "1.0000000+1.0000000i" "1.0000001+1.0000001i" "1.0000001+1.0000001i"
 [7] "1.0000001+1.0000001i" "1.0000001+1.0000001i" "1.0000001+1.0000001i"
[10] "1.0000001+1.0000001i"
prettyNum(fv, drop0trailing = TRUE) # a bit nicer
 [1] "1+1i"                 "1+1i"                 "1+1i"                
 [4] "1+1i"                 "1.0000001+1.0000001i" "1.0000001+1.0000001i"
 [7] "1.0000001+1.0000001i" "1.0000001+1.0000001i" "1.0000001+1.0000001i"
[10] "1.0000001+1.0000001i"
options(op)

## The  '  flag :
doLC <- FALSE # <= R warns, so change to TRUE manually if you want see the effect
if(doLC) {
  oldLC <- Sys.getlocale("LC_NUMERIC")
           Sys.setlocale("LC_NUMERIC", "de_CH.UTF-8")
}
formatC(1.234 + 10^(0:4), format = "fg", width = 11, flag = "'")
[1] "      2.234" "      11.23" "      101.2" "       1001" "      10001"
## -->  .....  "      1'001" "     10'001"   on supported platforms
if(doLC) ## revert, typically to  "C"  :
  Sys.setlocale("LC_NUMERIC", oldLC)

[Package base version 4.2.3 Index]