0

I'm working on a HackerRank samples. For some reasons, the output needs to have 1 decimal place even if the actual number is a round number.

For example, even though my answer is theoretically correct, which is 32, HackerRank considers it as false because 32 is not 32.0

I've also tried

format(32, nsmall = 1)

But this is still an error because the output comes with " ".

I've looked into Formatting Decimal places in R, but this doesn't answer my question.

The output must not be "32.0", it must be 32.0

enter image description here

enter image description here

enter image description here

Afiq Johari
  • 1,372
  • 1
  • 15
  • 28
  • I have no idea what "Hacker Rank samples" are and you don't say what exactly needs to be submitted and is tested. Maybe `print(format(32, nsmall = 1), quote = FALSE)`? – Roland Nov 28 '19 at 13:29
  • I'm working on this example https://www.hackerrank.com/challenges/s10-weighted-mean/problem, the expected result is 32.0. But my R output wouldn't show the decimal, therefore my answer is considered false. I tried your solution but it gives an error because of the [1] that comes with the output. – Afiq Johari Nov 28 '19 at 13:33
  • 1
    I'm not impressed by how they check results. Anyway, `cat(format(32, nsmall = 1))`. – Roland Nov 28 '19 at 13:34
  • 1
    @Roland, you're so kind with words. It's just too atrocious to me to be honest. But recruiters are using this platform to test out coding skills which I have to take. – Afiq Johari Nov 28 '19 at 13:36

2 Answers2

1

Thanks @Roland for the tips. The below is accepted.

cat(format(32, nsmall = 1))
Afiq Johari
  • 1,372
  • 1
  • 15
  • 28
0

I Think below code should work:

# devtools::install_github("renkun-ken/formattable")
library(formattable)
p <- formattable(as.numeric("32"), digits = 1, format = "f")
p
Harshal Gajare
  • 605
  • 4
  • 16