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 6200973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:28:02+00:00 2026-05-24T04:28:02+00:00

When I use sprintf(‘%E’,@value) for some large arbitrary value, e.g. 3.14158995322368e+22f it prints 3.14158995322368e+

  • 0

When I use

sprintf('%E',@value)

for some large arbitrary value,

e.g. 3.14158995322368e+22f

it prints

3.14158995322368e+ 0 22

How can I format the exponent? E.g. no leading 0 (2 digits) or always 3 or 4 digits (1 or 2 leading zeroes).

  • 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-24T04:28:03+00:00Added an answer on May 24, 2026 at 4:28 am

    Ruby’s sprintf is just a wrapper around the native libc snprintf. From sprintf.c (Ruby 1.9.2-p180):

    /*   
     *  call-seq:
     *     format(format_string [, arguments...] )   -> string
     *     sprintf(format_string [, arguments...] )  -> string 
     * [...]
     */
    
    VALUE
    rb_f_sprintf(int argc, const VALUE *argv)
    {
        return rb_str_format(argc - 1, argv + 1, GETNTHARG(0));
    }
    

    And inside rb_str_format, we find this:

    case 'f':
    case 'g':
    case 'G':
    case 'e':
    case 'E':
    case 'a':
    case 'A':
        /* ... */
        snprintf(&buf[blen], need, fbuf, fval);
    

    So you should get the same results on a single platform but not necessarily the same results on different platforms (even after taking the usual floating point issues into account). Ruby’s sprintf doesn’t offer any way to control the specific formatting of the exponent so you are at the mercy of the OS’s libc (i.e. you of luck, libc has no mercy).

    If necessary, you could use sprintf and then normalize the format with some greasy regex mangling. Not exactly a pleasant solution but you do what you have to do.

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

Sidebar

Related Questions

Can I use sprintf in a reentrant function if it writes in a local
I know about sprintf() , but how can I use the same parameter more
I'm attempting to use Printf.sprintf to print out a value that is within a
I want to use Perl's sprintf to zerofill a variable. sprintf(%08d, $var); But I
Is there a way to use the C sprintf() function without it adding a
Use case: I've just entered insert mode, and typed some text. Now I want
I'm trying to figure out how to use sprintf to print at least two
In C, is it possible to use recursion within the sprintf function ? For
I'm trying to export some data from MATLAB to a database. I use a
Trying to use as basic C as I can to build a list of

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.