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

  • Home
  • SEARCH
  • 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 9015281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:42:45+00:00 2026-06-16T03:42:45+00:00

I am writing a functional test in R for a program in C++. The

  • 0

I am writing a functional test in R for a program in C++. The typical output file has some columns as “string” and the other columns as “double”. Then, I would like to use diff to compare the expected output file returned by R with the observed output file returned by C++.

In pseudo-C++, I simply do this:

stringstream ssTxt;
ssTxt.precision (7);
ssTxt.setf (ios::scientific);
for(i=0; i<10; ++i){
  ssTxt << names[i] << " " << values[0][i] << " " << values[1][i] << " " << values[2][i] << endl;
  // write in file and clear ssTxt
}

Here is a typical output:

item1 2.8200000e-01 500 4.1846912e-04

In R, I do that:

results <- data.frame(name="item1", val1=0.282, val2=500, val3=0.00041846912873)
write.table(x=format(results, digits=8, nsmall=7, scientific=TRUE), file=...)

Here is the output corresponding to the same data:

item1 2.82e-01 500 4.1846912e-04

As you can see, it almost works, but R doesn’t add trailing 0 after “2.82”. I prefer to change the R code rather than the C++ code. So how can I do that?

  • 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-06-16T03:42:46+00:00Added an answer on June 16, 2026 at 3:42 am

    Have you tried
    sprintf or formatC?

    To be specific I think

    sprintf("%e", 2.82e-01) 
    

    should do the trick but as mentioned above have a look at help(sprintf), which describes the various formatting capabilities of this function …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a PHP program which reads file with file_get_contents then attempts to
I've been having some problems with a program that I've been writing and would
I'm currently writing a function what would create a zip file, which will be
i am writing a program to test WCF service performance in high concurrency circumstance.
I'm currently writing a little Stata program in an .ado file. Within this .ado
I am writing a common lisp program that needs to handle the output of
I'm writing a C++ program and I need some help understanding an error. By
I'm writing a program that implements the RFC 2544 network test. As the part
I'm writing a test program that use a Tab Controller with multiple tab views.
I'm writing a short function to parse through a file by checking string tokens.

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.