"1234" --> 1234
"605" --> 605
"1405" --> 1405
"-7" --> -7
18 Convert a String to a Number!
8kyu Tantangan #18/366 - 03 Mar 2024
https://www.codewars.com/kata/57356c55867b9b7a60000bd7
18.1 Instruction
Note: This kata is inspired by Convert a Number to a String!. Try that one too.
Description We need a function that can transform a string into a number. What ways of achieving this do you know?
Note: Don’t worry, all inputs will be strings, and every string is a perfectly valid representation of an integral number.
Examples
18.2 YouTube Video
18.3 Solution Code
<- function(x){
string_to_number as.numeric(x)
}
<- as.numeric string_to_number
18.4 Test
library(testthat)
test_that('Basic tests', {
expect_equal(string_to_number("1234"), 1234)
expect_equal(string_to_number("605"), 605)
expect_equal(string_to_number("1405"), 1405)
expect_equal(string_to_number("-7"), -7)
})
Test passed 😸