How can I paste 100000 without it being shortened to 1e+05? [duplicate]

StackOverflow https://stackoverflow.com/questions/13579953

  •  02-12-2021
  •  | 
  •  

문제

Question: How can I use paste without 100000 becoming 1e+05?

Sorry in advance if this question seems frivolous (but it has resulted in a bug in my code). I use R to call an external script, so when I say e.g. paste("abc",100000) I want it to output "abc 100000" and not "abc 1e+05".

Here's an example of what it looks like on my screen:

> paste("abc",100000)
[1] "abc 1e+05"
> paste("abc",100001)
[1] "abc 100001"

This results in the bizarre behaviour that my script works for the input "100001" but not "100000".

I realise I could create a script to convert numbers to strings however I like, but I feel I shouldn't do this if there is an internal way to do the same thing (I suspect there is some "method" I'm missing).

[If it helps, I'm on Ubuntu 12.04.1 LTS ("precise"), running R version 2.14.1 (2011-12-22) in a terminal.]

도움이 되었습니까?

해결책

See ?options, particularly scipen:

R> paste("abc", 100000)
[1] "abc 1e+05"
R> options("scipen"=10)    # set high penalty for scientific display
R> paste("abc", 100000)
[1] "abc 100000"
R> 

Alternatively, control formatting tightly the old-school way via sprintf():

R> sprintf("%s %6d", "abc", 100000)
[1] "abc 100000"
R> 

다른 팁

Alternatively, you can use integers which don't get printed in scientific notation. You can specify that your number is an integer by putting an "L" behind it, or doing as.integer.

> paste("abc",100000L)
[1] "abc 100000"
> paste("abc",as.integer(1000000000))
[1] "abc 1000000000"

Alternatively format may be simpler than sprintf especially when you want to change how decimals are displayed:

> paste("abc",format(100000, scientific = FALSE))
[1] "abc 100000"
> paste("abc",format(0.1234567, scientific = FALSE, digits = 4))
[1] "abc 0.1235"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top