Search code examples
rsumdigits

fix r sum() auto remove the small digital .05


i have a problem that when I use R sum() function, the sum() automatically remove the small number e.g. 0.05 in the total.

any suggestions on how to fix this? originally I use dplyr summarize(sum()) function.and i find the problem

dfexl %>% 
  filter(Text=='Totalt') %>%
  summarise( number_of_total = n(),
             grundbook_total = sum(Konto, na.rm = T))

and the simplest version is

sum(22068.00, 144501.00,  71153.00,  26193.05,  10395.00 , 80619.00)

it will output: 354929 rather than 354929.05

any suggestion?

i even use the following 2 methords, not able to fix

dfexl %>% 
  filter(Text=='Totalt') %>%
  summarise( number_of_total = n(),
             grundbook_total = round(sum(Konto * 100, na.rm = T)/100,4))

if I use grundbook_total*100 then I can see 35492905.

options(digits=4)

Thank you!


Solution

  • I believe this is just a printout issue; if you want to increase the number of significant digits in the printout, you could try:

    sprintf("%.2f",sum(22068.00, 144501.00,  71153.00,  26193.05,  10395.00 , 80619.00))
    # [1] "354929.05"
    

    And to change the number of digits, just change the number in the first argument, i.e.:

    sprintf("%.10f",sum(22068.00, 144501.00,  71153.00,  26193.05,  10395.00 , 80619.00))
    #[1] "354929.0500000000"