0

I want to extract just decimal part as a integer but it wasn't that easy as I thought. While searching I found that float/decimal aren't the things that we can trust upon.

Is floating point math broken? Here I got answer why they don't work as expected but how exactly can I get decimal part as number isn't mentioned. For Now just upto two decimals

 double number = 43475.1;
    int integerNumber = number;//implicit
    double difference = number-integerNumber;
    int floatingNumber  = difference*100;
    std::cout<<"NUMBER = "<<number<<"\nintegerNumber = "<<integerNumber;
    std::cout<<"\nDIFFERENCE INLINE "<< number-integerNumber<<"\nDIFFERENCE CALCULATED "<<difference;
    std::cout<<"\nDIFFERENCE num " << floatingNumber;

Here the output is

NUMBER = 43475.1
integerNumber = 43475
DIFFERENCE INLINE 0.1
DIFFERENCE INT 0.1
DIFFERENCE num 9

How can I get 10 instead of 9. I used a hack but I am not really convinced with my own answer

    std::string str = std::to_string(difference);
    int ans = atof(str.c_str())*100;
    std::cout<<"\n\nAFTER CONVERSIONS::: \n";
    std::cout<<str<<" and realDecimal= "<<ans;

This will result in

AFTER CONVERSIONS::: 
0.100000 and realDecimal= 10

This works but are there better solution than converting to string and again back to integer???

prosach
  • 312
  • 2
  • 14
  • 4
    Personally, I would use the string approach. Read in as a string, split on the decimal separator, and then convert to a 64 bit integer type. – NathanOliver Jan 24 '22 at 17:08
  • Do you mean the same or different? And I want to make sure that it is just a small portion of code in plain c++(extracted). What I get is `double` as argument and I need to get decimal from it. – prosach Jan 24 '22 at 17:12
  • 2
    @prosach -- Why do you need to do exact decimal math? If it's for the purpose of money or financial applications, then do not use `double`. No amount of finagling `double` will guarantee exact results. – PaulMcKenzie Jan 24 '22 at 17:15
  • @PaulMcKenzie It's actually for converting number to words and I want to convert both parts of number so I am trying this method – prosach Jan 24 '22 at 17:35
  • @prosach Perhaps if you describe your input and your expected output (the words you now mention) would it be easier to see if there's a nicer shortcut to what you actually want. – Ted Lyngmo Jan 24 '22 at 17:38
  • To break into fractional and integral parts there's `std::modf`. – MatG Jan 24 '22 at 18:06
  • @prosach -- *It's actually for converting number to words* -- Then you should use strings, not `double`. There is no need for `double` at all to do this. Basically, the conversion to words you're trying for is after the horse has left the barn, i.e. the `double` is already "off", and you're trying to convert an "off" value into words -- it's too late by then. Also, converting to words is usually done in money/financial apps (like check writing). This goes back to my first point about this being one of those types of applications. – PaulMcKenzie Jan 24 '22 at 18:55

1 Answers1

0

How can I get 1 instead of 9.

Firstly, multiply by 10 instead of 100. Secondly, round to nearest integer instead of towards zero by using std::round:

std::fesetround(FE_TONEAREST);
int floatingNumber  = std::round(difference*10);
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 2
    @prosach You are not doing what is mentioned in this answer though. You are multiplying `floatingNumber` by 100, not `difference`. – Ted Lyngmo Jan 24 '22 at 17:26
  • Yeah, It was my fault on putting the variable, But I have to include `` which I really don't know of. So Are there other process or this one is quite good based on memory. What I think of my method is conversion from decimal to string and again to decimal is quite lot of work? Is this method correct in most of the situations? – prosach Jan 24 '22 at 17:32