Round the given number to nearest multiple of 10 | Set-2
Last Updated :
22 Dec, 2022
Given a large positive integer represented as a string str. The task is to round this number to the nearest multiple of 10.
Examples:
Input: str = “99999999999999993”
Output: 99999999999999990
Input: str = “99999999999999996”
Output: 100000000000000000
Approach: A solution to the same problem has been discussed in this article which will not work for large numbers. When the number is large and represented as strings we can process the number digit by digit. The main observation is that if the last digit of the number is ? 5 then only the last digit will get affected i.e. it will be replaced with a 0. If it is something greater than 5 then the number has to be rounded to some next higher multiple of 10 i.e. the last digit will be replaced with a 0 and 1 will have to be added to the rest of the number i.e. the number represented by the sub-string str[0…n-1] which can be done by storing carry generated at every step (digit).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void roundToNearest(string str, int n)
{
if (str == "" )
return ;
if (str[n - 1] - '0' <= 5) {
str[n - 1] = '0' ;
cout << str.substr(0, n);
}
else {
int carry = 0;
str[n - 1] = '0' ;
int i = n - 2;
carry = 1;
while (i >= 0 && carry == 1) {
int currentDigit = str[i] - '0' ;
currentDigit += carry;
if (currentDigit > 9) {
carry = 1;
currentDigit = 0;
}
else
carry = 0;
str[i] = ( char )(currentDigit + '0' );
i--;
}
if (carry == 1)
cout << carry;
cout << str.substr(0, n);
}
}
int main()
{
string str = "99999999999999993" ;
int n = str.length();
roundToNearest(str, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void roundToNearest(StringBuilder str, int n)
{
if (str.toString() == "" )
return ;
if (str.charAt(n - 1 ) - '0' <= 5 )
{
str.setCharAt(n - 1 , '0' );
System.out.print(str.substring( 0 , n));
}
else
{
int carry = 0 ;
str.setCharAt(n - 1 , '0' );
int i = n - 2 ;
carry = 1 ;
while (i >= 0 && carry == 1 )
{
int currentDigit = str.charAt(i) - '0' ;
currentDigit += carry;
if (currentDigit > 9 )
{
carry = 1 ;
currentDigit = 0 ;
}
else
carry = 0 ;
str.setCharAt(i, ( char )(currentDigit + '0' ));
i--;
}
if (carry == 1 )
System.out.print(carry);
System.out.print(str.substring( 0 , n));
}
}
public static void main(String[] args)
{
StringBuilder str = new StringBuilder( "99999999999999993" );
int n = str.length();
roundToNearest(str, n);
}
}
|
Python3
def roundToNearest( str , n):
if ( str = = ""):
return
if ( ord ( str [n - 1 ]) - ord ( '0' ) < = 5 ):
str = list ( str )
str [n - 1 ] = '0'
str = ''.join( str )
print ( str [ 0 :n])
else :
carry = 0
str = list ( str )
str [n - 1 ] = '0'
str = ''.join( str )
i = n - 2
carry = 1
while (i > = 0 and carry = = 1 ):
currentDigit = ord ( str [i]) - ord ( '0' )
currentDigit + = carry
if (currentDigit > 9 ):
carry = 1
currentDigit = 0
else :
carry = 0
str [i] = chr (currentDigit + '0' )
i - = 1
if (carry = = 1 ):
print (carry)
print ( str [ 0 :n])
if __name__ = = '__main__' :
str = "99999999999999993"
n = len ( str )
roundToNearest( str , n)
|
C#
using System;
using System.Text;
class GFG
{
static void roundToNearest(StringBuilder str, int n)
{
if (str.ToString() == "" )
return ;
if (str[n - 1] - '0' <= 5)
{
str[n - 1] = '0' ;
Console.Write(str.ToString().Substring(0, n));
}
else
{
int carry = 0;
str[n - 1] = '0' ;
int i = n - 2;
carry = 1;
while (i >= 0 && carry == 1)
{
int currentDigit = str[i] - '0' ;
currentDigit += carry;
if (currentDigit > 9)
{
carry = 1;
currentDigit = 0;
}
else
carry = 0;
str[i] = ( char )(currentDigit + '0' );
i--;
}
if (carry == 1)
Console.Write(carry);
Console.Write(str.ToString().Substring(0, n));
}
}
public static void Main(String[] args)
{
StringBuilder str = new StringBuilder( "99999999999999993" );
int n = str.Length;
roundToNearest(str, n);
}
}
|
Javascript
function roundToNearest(str, n)
{
str = Array.from(str)
if ((str.join( "" )).localeCompare( "" ) == 0)
return ;
if (parseInt(str[n - 1]) <= 5)
{
str[n - 1] = '0' ;
process.stdout.write(str.join( "" ));
}
else
{
let carry = 0;
str[n - 1] = '0' ;
let i = n - 2;
carry = 1;
while (i >= 0 && carry == 1)
{
let currentDigit = parseInt(str[i]);
currentDigit += carry;
if (currentDigit > 9)
{
carry = 1;
currentDigit = 0;
}
else
carry = 0;
str[i] = String(currentDigit);
i--;
}
if (carry == 1)
process.stdout.write(1);
process.stdout.write(str.join( "" ));
}
}
let str = "99999999999999993" ;
let n = str.length;
roundToNearest(str, n);
|
Output:
99999999999999990
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...