Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

BigDecimal

Problem Description

The BigDecimal class provides operations on double numbers for arithmetic, scale handling, rounding, comparison, format conversion and hashing. It can handle very large and very small floating-point numbers with great precision but compensating with the time complexity a bit.

A BigDecimal consists of a random precision integer unscaled value and a 32-bit integer scale. If greater than or equal to zero, the scale is the number of digits to the right of the decimal point. If less than zero, the unscaled value of the number is multiplied by 10^(-scale).

Syntax:

Declaration

double a, b;  
BigDecimal A, B;

Initialization:

a = 5.4;  
b = 2.3;  
A = BigDecimal.valueOf(5.4);  
B = BigDecimal.valueOf(2.3);

If you are given a String representation of a double number then you can initialize in the following manner:

A = new BigDecimal(“5.4”);  
B = new BigDecimal(“1238126387123.1234”);

Extraction of value from BigDecimal:

// value should be in limit of double x  
double x = A.doubleValue();

// To get string representation of BigDecimal A  
String z = A.toString();

Comparison:

if (a < b) {} // For primitive double  
if (A.compareTo(B) < 0) {} // For BigDecimal

Actually compareTo returns -1(less than), 0(Equal), 1(greater than) according to values.

For equality we can also use:

if (A.equals(B)) {} // A is equal to B  

To read more about BigDecimal and its methods Click Here.

Task:

Given an array, s, of n real number strings, sort them in descending order — but wait, there's more! Each number must be printed in the exact same format as it was read from stdin, meaning that .1  is printed as .1, and 0.1 is printed as 0.1. If two numbers represent numerically equivalent values (e.g. .1 is equivalent to 0.1), then they must be listed in the same order as they were received as input).

Complete the code in the editor below. You must rearrange array s's elements according to the instructions above.

Problem Constraints

1 <= n <= 200

Each si has atmost 300 digits.



Input Format

The first line consists of a single integer,n, denoting the number of integer strings.

Each line i of the n subsequent lines contains a real number denoting the value of si .



Output Format

Pre-written code in the editor will print the contents of array s to stdout. You are only responsible for reordering the array's elements.



Example Input

Sample Input:

 9
 -100
 50
 0
 56.6
 90
 0.12
 .12
 02.34
 000.000



Example Output

Sample Output:

 90
 56.6
 50
 02.34
 0.12
 .12
 0
 000.000
 -100



Start solving BigDecimal on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test