Round the given number to nearest multiple of 10 | Java code for above approach ; Program to round the number to the nearest number having one 's digit 0 ; last character is 0 then return the original string ; if last character is 1 or 2 or 3 or 4 or 5 make it 0 ; process carry ; return final string ; Driver Code ; Function Call
import java . io . * ; class GFG { public static String round ( String s , int n ) { char [ ] c = s . toCharArray ( ) ; if ( c [ n - 1 ] == '0' ) return s ; else if ( c [ n - 1 ] == '1' c [ n - 1 ] == '2' c [ n - 1 ] == '3' c [ n - 1 ] == '4' c [ n - 1 ] == '5' ) { c [ n - 1 ] = '0' ; return new String ( c ) ; } else { c [ n - 1 ] = '0' ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( c [ i ] == '9' ) c [ i ] = '0' ; else { int t = c [ i ] - '0' + 1 ; c [ i ] = ( char ) ( 48 + t ) ; break ; } } } String s1 = new String ( c ) ; if ( s1 . charAt ( 0 ) == '0' ) s1 = "1" + s1 ; return s1 ; } public static void main ( String [ ] args ) { String s = "5748965412485599999874589965999" ; int n = s . length ( ) ; System . out . println ( round ( s , n ) ) ; } }