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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:24:39+00:00 2026-05-26T01:24:39+00:00

I have a function which should covert a double value into string one: inline

  • 0

I have a function which should covert a double value into string one:

inline static string StringFromNumber(double val) // suppose val = 34.5678
{
 long integer = (long)val; // integer = 34
 long pointPart; // should be = 5678  how do I get it?
}

How do I get a long value for both integer and pointPart?

Add: I want a precision of 17 numbers, with discarding the zeros. More examples:

val = 3.14 integer = 3 pointPart = 14

val = 134.4566425814748 integer = 134 pointPart = 4566425814748

I have not got any solution so far. How can I get it?

  • 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-26T01:24:40+00:00Added an answer on May 26, 2026 at 1:24 am

    You can use modf to separate the integer and fractional parts. You can then multiply the fractional part by 1.0e17, and call floor to properly round the results to it’s integer component, and then cast to a unsigned long (the fractional part will never be negative, and this allows you to maximize the number of bits in the integral type). Finally run though a loop to trim off the zeros on the unsigned long. For instance:

    inline static string StringFromNumber(double val)
    {
        double intpart, fracpart;
        fracpart = round((modf(val, &intpart)) * 1.0e17);
    
        long int_long = static_cast<long>(intpart);
        unsigned long frac_long = static_cast<long>(fracpart);
    
        //trim off the zeros
        for(unsigned long divisor = 10;;divisor *= 10)
        {
            if ((frac_long / divisor) * divisor != frac_long)
            {
                frac_long = frac_long / (divisor / 10);
                break;
            }
        }
    
        //...more code for converting to string
    }
    

    Note that this code will only work up to 17 decimal places if you are on a 64-bit platform and unsigned long is defined as a 64-bit integer-type. Otherwise you will want to change unsigned long to uint64_t. Also keep in mind that since floating point numbers are approximations, and there’s a multiplier by 1.0e17, the value of fracpart may not be exactly the value of the point-part of val … in other words there may be some additional digits after any necessary rounding.

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

Sidebar

Related Questions

I have a function which parses one string into two strings. In C# I
I have a function to create a pdf which should return bitarray. Below is
I have a function which accepts a string parameter such as: var1=val1 var2=val2 var3='a
I have a function which returns a one-sided intersection of values between two input
I have this function which should run a shell command. This is how I
I have a SQL Server function which converts a nvarchar day duration setting into
I have a function which searches an STL container then returns the iterator when
I have an function which decodes the encoded base64 data in binary data but
I have a function which gets a key from the user and generates a
i have a function which retrieves values from a webservice , then loops through

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.