Adjust code in R

I would like to insert two decimal places in output. Value refers to the value in meters and output is the value in kilometers, however after I do output I have zero result for the first example, however I would like it to be 0.23 Km . I left it round, because in some cases it makes sense, as for the second example, which was rounded to 1 km. How can I adjust this?

Example 1

value1 = 233
output1<-round(value1/1000)
output1
> output1
[1] 0

Example2

value2 = 970
output2<-round(value2/1000)
output2
> output2
[1] 1

Maybe using the signif() function would get you what you want.

value1 = 233
output1<-signif(value1/1000,digits = 1)
output1
[1] 0.2

value2 = 970
output2<-signif(value2/1000,digits = 1)
output2
[1] 1

Perfect, thanks @FJCC !

Just need to give round an argument to get what you're after.

value1 = 233
output1<-round(value1/1000, 2)
output1
[1] 0.23

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.