Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by Round ( 6 years ago )
#include <iostream>
#include <string>
using namespace std;

string Round(string &s)
{
	int length_s = s.length(), check = 0;
	int decimalPoint;
	//Increase 1 if the number greater and equal 5
	if(s[length_s - 1] >= '5')
	{
		//Starting loop each index from the last third index
		s[length_s - 1] = '0';
		check = 1;
		for(int i = length_s - 2; i >= 0; i--)
		{
			//if number = '9', turn it to '0' and check = 1
			if(s[i] == '9' && check == 1)
			{
				s[i] = '0';
				check = 1;
			}
			//if number != '9', turn check = 0 to avoild logical issue 
			else if(s[i] != '.' && check == 1)
			{
				s[i] = ((s[i] - 48 ) + 1) + 48;
				break;
			}
		}
	}

	return s;
}

int main()
{
  	string s = "19.99999795";
	string result = Round(s);
	cout << "Result: " << result;

	system("pause");
	return 0;
}

 

Revise this Paste

Children: 108351
Your Name: Code Language: