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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:07:16+00:00 2026-05-23T00:07:16+00:00

I’d like to write my own lexical_cast which preserves the decimal point when converting

  • 0

I’d like to write my own lexical_cast which preserves the decimal point when converting double to std::string. So I’m using ostringstream and set the flag std::ios::showpoint:

#include <string>
#include <iostream>
#include <boost/lexical_cast.hpp>

template <typename Source>
std::string my_string_cast(Source arg){
   std::ostringstream interpreter;
   interpreter.precision(std::numeric_limits<Source>::digits10);
   interpreter.setf(std::ios::showpoint);
   interpreter << arg;
   return interpreter.str();
}

int main(int argc, char** argv) {
   std::cout << my_string_cast(1.0) << std::endl;
   std::cout << my_string_cast(5.6) << std::endl;
   std::cout << my_string_cast(1.0/3.0) << std::endl;
   return 0;
}

This, however, prints unnecessary 0 digits, a behaviour I’d expect from setting std::ios::fixed but not std::ios::showpoint:

1.00000000000000
5.60000000000000
0.333333333333333

Without setting std::ios::showpoint it gives

1
5.6
0.333333333333333

but I want something like this:

1.0
5.6
0.333333333333333

Any easy way?

  • 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-23T00:07:17+00:00Added an answer on May 23, 2026 at 12:07 am

    After a long time looking through the code of the std library it seems everything gets handed over to some printf type function: __builtin_vsnprintf(__out, __size, __fmt, __args). The format string __fmt is set depending on the flags set on the ostringstream object and can be queried using

    std::ostringstream s;
    // ...
    char format[50];
    std::__num_base::_S_format_float(s,format,'\0');
    

    The default format string is %.*g which is used as in printf("%.*g",precision,x); where precision is an int and x the double to be printed. For the other flags we get:

    s.setf(std::ios::fixed);      // %.*f
    s.setf(std::ios::showpoint);  // %#.*g
    

    Yet the format %#g doesn’t just keep the decimal point but also keeps all trailing zeros. The doc says about the usage of # combined with g:

    "printf" will always print out a decimal point and trailing zeros will not
    be removed; usually 'g' and 'G' remove trailing zeros.
    

    Unfortunately, I can’t find any other printf format string which behaves as nice as %g but always keeps the decimal point, so I guess something along the lines of dschaeffer’s answer might well be the best.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Specifically, suppose I start with the string string =hello \'i am \' me And

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.