Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6900025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:29:43+00:00 2026-05-27T07:29:43+00:00

I am just starting to write some documents with Sweave/R and I like the

  • 0

I am just starting to write some documents with Sweave/R and I like the \sexpr{} command that lets one tow write numbers directly within text.

If I have a number like mus=0.0002433121, well I can say round it to a number of decimal places e.g.

\Sexpr{round(mus,7)}

How to write it in the scientific notation i.e. as LaTeX would be outputting

2.43 \times 10^{-4} 

and can we control the number of significant digits to be outputted like 3 in this example?

I note that a number like sigma = 2000000 is written automatically to 2e + 06 if I specify

\Sexpr{round(sigma,2)}. 

I would prefer that it would be written as

2 \times 10^6 

same as we would get in LaTeX notation and perhaps giving us the possibility to control the number of significant digits as well.

How to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:29:44+00:00Added an answer on May 27, 2026 at 7:29 am

    I think this function should work:

    sn <- function(x,digits)
    {
      if (x==0) return("0")
      ord <- floor(log(abs(x),10))
      x <- x / 10^ord
      if (!missing(digits)) x <- format(x,digits=digits)
      if (ord==0) return(as.character(x))
      return(paste(x,"\\\\times 10^{",ord,"}",sep=""))
    }
    

    Some tests:

    > sn(2000000)
    [1] "2\\\\times 10^{6}"
    > sn(0.001)
    [1] "1\\\\times 10^{-3}"
    > sn(0.00005)
    [1] "5\\\\times 10^{-5}"
    > sn(10.1203)
    [1] "1.01203\\\\times 10^{1}"
    > sn(-0.00013)
    [1] "-1.3\\\\times 10^{-4}"
    > sn(0)
    [1] "0"
    

    If you want the result in math mode you could enter $ signs in the paste() call.

    Edit:

    Here is a Sweave example:

    \documentclass{article}
    
    \begin{document}
    <<echo=FALSE>>= 
    sn <- function(x,digits)
    {
      if (x==0) return("0")
      ord <- floor(log(abs(x),10))
      x <- x / 10^ord
      if (!missing(digits)) x <- format(x,digits=digits)
      if (ord==0) return(as.character(x))
      return(paste(x,"\\\\times 10^{",ord,"}",sep=""))
    }
    @
    
    Blablabla this is a pretty formatted number $\Sexpr{sn(0.00134,2)}$.
    
    \end{document}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone done something like this? How? I'm just starting a project that will
Just as some background, I intend to write an Excel add-in with .NET that
I am just starting to write ruby rails. I wrote a controller but is
Just starting out, this should be a simple one but I haven't been able
Im just starting with localization today and need some information. I have a project
I just starting reading about SEO and realized that I should change my GET
Just starting out with an iPhone application using xcode 4.2. I understand that it
I'm just starting out with postsharp/AOP. I want to make some instrumentation for C#
I currently have a list of documents, displayed one after another. When some of
So, I'm starting to write some logic for a simple program (toy game on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.