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>
#include <iomanip>
using namespace std;

string Round(string &s)
{
	int length_s = s.length(), check = 0;
	int decimalPoint;

	for(int j = 0; j < length_s; j++)
	{
		if(s[j] == '.')
		{
			check = 2;
			break;
		}
	}

	//Increase 1 if the number greater and equal 5
	if(s[length_s - 1] >= '5' && check == 2)
	{
		//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;
				check = 0;
				break;
			}
		}

		if(check == 1)
		{ s.insert(0,"1");}

	}
	return s;
}

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

	system("pause");
	return 0;
}

 

Revise this Paste

Parent: 108575
Your Name: Code Language: