Problem Statement
We need to compute the result of the addition of two numbers. The result must be rounded to n significant digits, where n is determined as follows:
First determine the number with the least number of digits to the right of the decimal point (this may be zero). Then the result will not contain any significant digits to the right of this digit, and all other digits are significant (except leading zeros that are two or more places to the left of the decimal point). For example, the result of 1.56 + 1236.1 will have only one significant digit immediately to the right of the decimal point, but all digits to the left of the decimal point will be significant.
There are several cases to consider when rounding a number to n significant digits:
1) If the number has less than n significant digits, the number is padded with trailing zeros. For example, 2.5 rounded to 3 significant digits is 2.50.
2) If the digit immediately to the right of the nth digit is greater than 5, the number is rounded up. For example, 2.56 rounded to 2 significant digits is 2.6.
3) If the digit immediately to the right of the nth digit is less than 5, the number is rounded down. For example, 2.54 rounded to 2 significant digits is 2.5.
4) If the digit immediately to the right of the nth digit is 5 and there are non-zero digits after the 5, the number is rounded up. For example, 2.551 rounded to 2 significant digits is 2.6.
5) If the digit immediately to the right of the nth digit is 5 and there are no subsequent non-zero digits, then the number is rounded whichever way leaves the nth digit even. For example, 2.55 rounded to 2 significant digits is 2.6, but 2.45 rounded to 2 significant digits is 2.4.
You will be given an expression as a
Your result should be in the same format as described above. However, there should only be a leading zero if the integral part of the number is 0. For example, 0.00450 is a valid result, while 0459.2 is not.
Definition
- Class:
- SignificanceArithmetic
- Method:
- evaluate
- Parameters:
- String
- Returns:
- String
- Method signature:
- String evaluate(String expression)
- (be sure your method is public)
Notes
- Numbers in expression may contain leading zeros.
Constraints
- expression will contain two numbers that are separated by a '+'.
- Each number in expression will be a real number containing between 1 and 6 digits, inclusive (not including the decimal point).
- Each number in expression will be formatted exactly as described in the problem statement.
Examples
"2+2"
Returns: "4"
Two has one significant digit, so the result will as well.
"1.234+0.000"
Returns: "1.234"
All digits here are significant.
"1.234+0.006"
Returns: "1.240"
Again, all digits here are significant. Therefore the result will contain the same number of significant digits, and rounding rule #1 is used.
"12.57+2.6"
Returns: "15.2"
12.45 + 2.6 = 15.17. Rounding rule #2 is used.
"5.50005+0"
Returns: "6"
The only significant digit here is the one immediately to the left of the decimal point, and rounding rule #4 is used.
"2.5+2"
Returns: "4"
Again, the only significant digit here is the one immediately to the left of the decimal point. Here, rounding rule #5 is used.
"999999+0.900"
Returns: "1000000"
"1.0001+0.9999"
Returns: "2.0000"
"0.00+000"
Returns: "0"
"1.5+0"
Returns: "2"
"1.51+0"
Returns: "2"
"2.5+0"
Returns: "2"
"0.25+0"
Returns: "0"
"0.25+0.0"
Returns: "0.2"
"0.00001+0.00009"
Returns: "0.00010"
"0.0+0.0"
Returns: "0.0"
"0+0.84994"
Returns: "1"
"0.99+1.01"
Returns: "2.00"
"0.2+1.48"
Returns: "1.7"
"4.44+0.2"
Returns: "4.6"
"0.23+5.22501"
Returns: "5.46"
"1.3+0.25"
Returns: "1.6"
"1.3+0.35"
Returns: "1.6"
"0.99+0.01"
Returns: "1.00"
"0.2+0.48"
Returns: "0.7"
"0.2+0.44"
Returns: "0.6"
"0.23+0.22005"
Returns: "0.45"
"0.25+0.3"
Returns: "0.6"
"0.35+0.3"
Returns: "0.6"
"0+0"
Returns: "0"
"000+000"
Returns: "0"
"0+1"
Returns: "1"
"0+0.1"
Returns: "0"
"0+0.6"
Returns: "1"
"0+0.4"
Returns: "0"
"0.5001+0"
Returns: "1"
"0+0.5"
Returns: "0"
"01.98+0.02"
Returns: "2.00"
"05.2+2.6"
Returns: "7.8"
"0002.2+0.2"
Returns: "2.4"
"4.4505+00003.1"
Returns: "7.6"
"004.15+0.2"
Returns: "4.4"
"004.25+0.2"
Returns: "4.4"
"999999+999999"
Returns: "1999998"
"1+999999"
Returns: "1000000"
"123+456"
Returns: "579"
"009+0"
Returns: "9"
"500+00500"
Returns: "1000"
"999999+0.5"
Returns: "1000000"
"0.00000+01.0001"
Returns: "1.0001"
"0.00009+0.00001"
Returns: "0.00010"
"0.00000+0.00001"
Returns: "0.00001"
"6408.19+81482.1"
Returns: "87890.3"
"14122.3+601327"
Returns: "615449"
"5.77167+2154.46"
Returns: "2160.23"
"577964+0.89146"
Returns: "577965"
"64066+4706.74"
Returns: "68773"
"4.27071+795.405"
Returns: "799.676"
"60.3820+60.6559"
Returns: "121.0379"
"509834+9.70502"
Returns: "509844"
"1.84565+396875"
Returns: "396877"
"3.36085+6169.95"
Returns: "6173.31"
"0.4146+643.418"
Returns: "643.833"
"26.2826+1401.41"
Returns: "1427.69"
"202.563+92.2725"
Returns: "294.836"
"12800.9+13.9656"
Returns: "12814.9"
"82498.4+68.238"
Returns: "82566.6"
"85414+5.78276"
Returns: "85420"
"94311.4+36048"
Returns: "130359"
"39.3798+286609"
Returns: "286648"
"18.2227+4.94041"
Returns: "23.1631"
"136133+158.14"
Returns: "136291"
"258.812+8812.92"
Returns: "9071.73"
"91.3922+47.871"
Returns: "139.263"
"94.9966+71.6394"
Returns: "166.6360"
"423.712+67.1592"
Returns: "490.871"
"9.48534+438951"
Returns: "438960"
"12.7528+8605.39"
Returns: "8618.14"
"3950.67+12578.0"
Returns: "16528.7"
"1624.91+4.92270"
Returns: "1629.83"
"4.41672+8311.43"
Returns: "8315.85"
"951.320+454.213"
Returns: "1405.533"
"665.880+88061"
Returns: "88727"
"9.25400+27.3379"
Returns: "36.5919"
"12857.4+16976.4"
Returns: "29833.8"
"74760.5+96.72"
Returns: "74857.2"
"7465.61+949.587"
Returns: "8415.20"
"4.55918+4703.31"
Returns: "4707.87"
"78.8214+500.17"
Returns: "578.99"
"7.82339+869161"
Returns: "869169"
"3282.2+2.81021"
Returns: "3285.0"
"776.94+0.49295"
Returns: "777.43"
"501.798+7.7792"
Returns: "509.577"
"358.50+8.12127"
Returns: "366.62"
"79265.7+6076.86"
Returns: "85342.6"
"27488.1+96424.3"
Returns: "123912.4"
"163836+575.376"
Returns: "164411"
"47.4684+477153"
Returns: "477200"
"7497.55+1.20069"
Returns: "7498.75"
"716.702+9485.71"
Returns: "10202.41"
"77893.1+697.379"
Returns: "78590.5"
"1992.86+41402.6"
Returns: "43395.5"
"27432.6+794.199"
Returns: "28226.8"
"21761.7+93.3480"
Returns: "21855.0"
"995389+5482.6"
Returns: "1000872"
"792.825+8884.66"
Returns: "9677.48"
"326885+58057.6"
Returns: "384943"
"329757+861.239"
Returns: "330618"
"0.36178+6412.73"
Returns: "6413.09"
"3559.4+84.3855"
Returns: "3643.8"
"51346.0+4621.38"
Returns: "55967.4"
"1.23301+494943"
Returns: "494944"
"99+0.6"
Returns: "100"
"0200+000.4"
Returns: "200"
"346+0.51"
Returns: "347"
"345+0.51"
Returns: "346"
"346+0.5001"
Returns: "347"
"345+0.5001"
Returns: "346"
"123+0.5"
Returns: "124"
"124+0.5"
Returns: "124"
"123+0.50"
Returns: "124"
"124+0.50"
Returns: "124"
"124+00.5000"
Returns: "124"
"123+00.5000"
Returns: "124"
"74508.2+724.399"
Returns: "75232.6"
"22378.0+8695.94"
Returns: "31073.9"
"552.983+274.304"
Returns: "827.287"
"71.9902+3.64956"
Returns: "75.6398"
"513072+8.83621"
Returns: "513081"
"1241.61+85.984"
Returns: "1327.59"
"18.2368+89.482"
Returns: "107.719"
"9837.37+12063.7"
Returns: "21901.1"
"31409+49.5775"
Returns: "31459"
"5435.69+83.7178"
Returns: "5519.41"
"0.04+0.0"
Returns: "0.0"
"0.04+0.02"
Returns: "0.06"
"983.17+76.8"
Returns: "1060.0"
"0.52+0"
Returns: "1"
"0.0053+00.02"
Returns: "0.03"
"0.00053+00.002"
Returns: "0.003"
"7.4772+7.62"
Returns: "15.10"
"0.00050+00.003"
Returns: "0.004"
"0.00050+00.002"
Returns: "0.002"
"3.00050+02.002"
Returns: "5.002"
"0.1202+0.12647"
Returns: "0.2467"
"1.257+15.56"
Returns: "16.82"
"10.885+10.11"
Returns: "21.00"
"9999.18+0.8"
Returns: "10000.0"
"2.35+0.1"
Returns: "2.4"
"2.25+2.2"
Returns: "4.4"
"14.502+0"
Returns: "15"
"3.5+2"
Returns: "6"
"4.00001+5.55554"
Returns: "9.55555"
"97.951+2.0"
Returns: "100.0"