1

Is there any way to determine how many numbers are there in my double after decimal point. for example double a=3.14259

If I make a new int b now, how can I make value of bequal to numbers after decimal point from a?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Malcom98.
  • 65
  • 1
  • 7
  • 1
    You might be able to use [modf](http://www.cplusplus.com/reference/cmath/modf/). – Javia1492 Aug 30 '17 at 19:04
  • 1
    Sounds like an [XY Problem](http://xyproblem.info/). What high-level problem are you actually trying to solve? – PaulMcKenzie Aug 30 '17 at 19:05
  • 1
    be careful not to confuse the number of digits you see on the screen when you do `std::cout << a;` with the number of significant digits of `a` – 463035818_is_not_an_ai Aug 30 '17 at 19:06
  • What does "numbers after decimal point" even mean? –  Aug 30 '17 at 19:06
  • Note that the amount of numbers after decimal point may be not the same as amount of numbers after decimal point of literal used to initialize this value. – user7860670 Aug 30 '17 at 19:07
  • Be careful - setting double a=3.14259 doesn't guarantee the number is going to look like that when it's printed. That's just not how floating point works. – Kevin Aug 30 '17 at 19:08
  • 3
    Floating point is inexact, thus your question needs clarification. – PaulMcKenzie Aug 30 '17 at 19:09
  • [Here](https://stackoverflow.com/a/33494853/1460794) is an example of how, even with a multiprecision library, things go wrong as soon as the compiler interprets the floating point literal. By wrong, I mean that the value might be as close as it can be in memory, but the decimal digits are no longer the same. So counting them, rounding them etc. will not provide the true answer. – wally Aug 30 '17 at 19:10
  • The problem is: Enter number f(double), and enter number n(int). N determines how many decimal places f will have. input f=3.457 | n=2 output 3.46 input f=3.454 i n=2 output 3.45 – Malcom98. Aug 30 '17 at 19:12
  • @Malcom98. What? – Javia1492 Aug 30 '17 at 19:13
  • Are you trying to format a floating point type? – konsolas Aug 30 '17 at 19:17
  • @Malcom98. have you tried `std::setprecision` ? – pmaxim98 Aug 30 '17 at 19:27
  • Do you realize that your question is completely different than what you need? – Slava Aug 30 '17 at 19:27
  • @pmaxim98 yes, but I haven't found a way to use it except with the line where's "cout". – Malcom98. Aug 30 '17 at 19:33
  • @Slava yes, but I'm gonna try to apply any possible response on my post to my program. – Malcom98. Aug 30 '17 at 19:34
  • if you need reponse to your question then formulate it properly, what you asked does not have solution, as stated in @stefanbachert answer, what you actually need has trivial solution though. – Slava Aug 30 '17 at 19:51

4 Answers4

2

The short answer is, you can't.

first of all, a type like double has always the same number of binary digits after a (binary) period. That is called mantissa. in case of double that are 53 bits, meaning 52 bit after binary period, in decimal that are about 15 digits. For details you may have a look a IEEE_754 (double precision)

When you convert a double to a decimal string, you will in general never match exact decimal. For example the value 0.1 could not exactly represented by a double value. a printf may show "0.1" after applying rounding.

However, when you are dealing with some double calculations you will experience small deriviation like 0.10000000000012 or 0.09999999999987. What will you do in such cases?

And there is a mathematican problem which has nothing to do with c++ doubles:

                     _
  0.1 is equal to 0.09

so your answer would either 1 or infinity

stefan bachert
  • 9,413
  • 4
  • 33
  • 40
0

Here is one way to do it: Convert the decimal to a string, and find the size of the substring after the decimal point, as follows:

#include <iostream>
#include <string>

int main()
{
    double a = 3.14259;
    std::string a_string = std::to_string(a);
    a_string.erase ( a_string.find_last_not_of('0') + 1, std::string::npos ); //Get rid
                                                                      //of extra zeroes
    std::cout << a_string.substr(a_string.find('.') + 1).size() << "\n";
    //Print the size of the substring of the original string from the decimal point (here 5)
}
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
0

You could treat the fractional part as a string literal. Utilize the std::stringstream and std::string:

#include <iostream>
#include <string>
#include <sstream>
int main(){
    double whole = 3.14259;
    auto fractionalno = whole - static_cast<long>(whole); // or use std::modf()
    std::stringstream ss;
    ss << fractionalno;
    std::string s = ss.str();
    s.erase(0,2);
    std::cout << "The number of digits is: " << s.size();
}
Ron
  • 14,674
  • 4
  • 34
  • 47
-2

Floating point number does not provide the number of digits after the point. (It is not even the 'exact' value. It is an approximate value.)

But if you just want to make another integer to have the same number of digits after the point just on the screen, why don't you just count?

Here is Python code:

a = 4.0 / 7
# a is 0.5714285714285714 
b = str(a)
# b (str(a)) is 0.571428571429, different with a.
nod = len(b[b.index('.'):])
_c = 9.0 / 7
c = float(int(_c * 10 ** nod)) / 10 ** nod
ghchoi
  • 4,812
  • 4
  • 30
  • 53