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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:44:06+00:00 2026-06-12T18:44:06+00:00

I’m investigating the replacing of doubles in our code with g++’s std::decimal::decimal32/64/128 types for

  • 0

I’m investigating the replacing of doubles in our code with g++’s std::decimal::decimal32/64/128 types for purpose of currency amounts and prices, however I’m getting stuck on the point of how to best input and output the data. Specifically, there do not appear to be any routines for converting to/from a string, and the stringstream mechanisms do not compile for these types. The only way I see doing this is to use double as an intermediate type, however surely this at least partially defeats the purpose of using the decimal types if we’re always inputting and outputting via doubles?

I’m sure I’m not understanding something here so would welcome some feedback on how best to use these types.

Edit:

I have hacked together a couple of input/output routines, however I’m not really satisfied with either. The input is hardly robust (no scientific notation support) and the output routine is simplistic, not to mention inefficient due to the double conversion.

#define MAX_DEC_LEN 17

std::decimal::decimal64 FromString(const char* str)
{
    if (strlen(str) > MAX_DEC_LEN)
        throw std::runtime_error("bad input");
    char buf[MAX_DEC_LEN+1];
    strncpy(buf, str, MAX_DEC_LEN+1);
    char* point(NULL); 
    if ((point = strchr(buf, '.')) != NULL)
        *(point++) = '\0';
    std::decimal::decimal64 ret(atoi(buf));
    if (point != NULL && *point != '\0')
    {
        int exponent(strlen(point));
        long long unsigned coeff(atoi(point));
        std::decimal::decimal64 d2(std::decimal::make_decimal64(coeff, -exponent));
        if (*buf == '-')
            ret -= d2;
        else
            ret += d2;
    }
    return ret;    
}

std::string ToString(std::decimal::decimal64 dec)
{
    double d(std::decimal::decimal_to_double(dec));
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(6) << d;
    return oss.str();
}

I’m really after something better for both of these, plus a round (to a particular precision) to round things off (pardon the pun)

  • 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-12T18:44:07+00:00Added an answer on June 12, 2026 at 6:44 pm

    The Decimal TR defines overloads for input and output in sections 3.2.10 and 3.2.11, respectively. From the looks of it they are not implemented by gcc. The decimal support in gcc is implemented in terms of libdecnum which provides routines to convert from and to strings. I would create a simple implementation of the I/O operators using them to get started.

    Edit on 2012-10-17: Looking at formatting the decimal values in gcc’s library (std::decimal::decimal64 and family) revealed that gcc unfortunately doesn’t install either the decNumber library headers or the library. Furthermore, other sources of decNumber don’t quite align with the version shipping with gcc. As a result, the only way I found so far to get decimal numbers formatted is to use explicit declarations and using the library build during compiling gcc.

    The simple part is the source, although for a rather simple implementation. First, the declaration which would go into a suitable header, e.g. <decimal/decimal>:

    std::ostream& operator<< (std::ostream&, std::decimal::decimal32 const&);
    std::ostream& operator<< (std::ostream&, std::decimal::decimal64 const&);
    std::ostream& operator<< (std::ostream&, std::decimal::decimal128 const&);
    

    A fairly simple implementation of something doing some formatting, although without any options could look like this:

    extern "C" char* decimal32ToString(std::decimal::decimal32 const*, char*);
    extern "C" char* decimal64ToString(std::decimal::decimal64 const*, char*);
    extern "C" char* decimal128ToString(std::decimal::decimal128 const*, char*);
    
    std::ostream& operator<< (std::ostream& out,
                              std::decimal::decimal32 const& value)
    {
        char buffer[128];
        decimal32ToString(&value, buffer);
        return out << buffer;
    }
    
    std::ostream& operator<< (std::ostream& out,
                              std::decimal::decimal64 const& value)
    {
        char buffer[128];
        decimal64ToString(&value, buffer);
        return out << buffer;
    }
    
    std::ostream& operator<< (std::ostream& out,
                              std::decimal::decimal128 const& value)
    {
        char buffer[128];
        decimal128ToString(&value, buffer);
        return out << buffer;
    }
    

    With this in place, there is still an issue that I didn’t manage to build anything using decimals unless some level of optimization, i.e., -O1 (or higher), is used. There are unreferenced symbols unless optimization is used but this is entirely unrelated to printing the values. To get definitions of the functions used in the implementation, I needed to link to the libdecnumber.a library which is created while gcc is being build:

    g++ -o prog decimal-io.cpp main.cpp <gcc-build-root>/libdecnumber/libdecnumber.a
    

    Aside from this I have my own implementation of the Decimal TR with some extension and basing it on the decnumber library and implement I/O properly. My implementation hopefully will become publicly available at some point but not any time soon.

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

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.