-1
float number=15.555555;
printf("FIRST FIVE NUMBERS%f",number); 

Desired ouput:

FIRST FIVE NUMBERS15555

Actual output:

FIRST FIVE NUMBERS15.555555

How do I get the 5 digit number and discard the decimal point?

For example:

123.43213 -> 12343

1.5634567 -> 15634

Community
  • 1
  • 1

2 Answers2

1

Without elegance, some brute force and rounding, you might do:

#include <cmath>
#include <iostream>

int main()
{
    double number = 15.555555;
    while(100000 <= number) number /= 10;
    while(number < 10000) number *= 10;
    // 10000.x <= number < 100000
    number = round(number);
    if(100000 <= number) number = round(number/10);
    std::cout << number << '\n';
}

Note: Only for numbers greater than zero.

0

You can always do something like this:

cout << "FIRST FIVE NUMBERS" << to_string(15.555555).substr(0, 5);

That won't round or anything but maybe it's what you want?

EDIT:

Per Lightness Races in Orbit's comment I think the desired result can be obtained from:

string foo = to_string(15.555555);

for_each(foo.begin(), advance(foo.begin(), min(5, foo.size())), [](const char& i){if(i != '.')cout << i});
Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288