0

I am writing a python code where I provide 2 strings as input and the program should give me sum of those numbers as response in string format.

def sum(s1, s2):
    return str(float(s1) + float(s2))

This works fine if my input s1 and s2 are small float values example 3.6, 2.1 gives me 5.7

But I have some issues here:

case 1:

IF input is s1=3, s2 = 5, I want output as 8, but the program gives me 8.0

case 2:

If input is s1=100000000000000000000000000000001 and s2 = 100000000000000000000000000000002, I want output as 200000000000000000000000000000003, but the program gives me 2e+32

What changes I have to do to this code? can you please help me.

I am using Python 3.

learner
  • 6,062
  • 14
  • 79
  • 139
  • 1
    Regarding floats, you can use the `float.is_integer()` method and round to nearest integer if that is true. You can also use string formatting to output a consistently formatted number. https://stackoverflow.com/questions/22617/format-numbers-to-strings-in-python – jkr Oct 13 '20 at 22:00
  • 1
    The reason you're getting floats back, is because you're setting them to floats. Floats also use notation for large/small numbers by default, so that is the reason for getting `2e+32` instead of the numbers themselves. Change `float` to `int` instead and you should get the expected results. – Hampus Larsson Oct 13 '20 at 22:02
  • 2
    @jakub when a `float` becomes large enough, `is_integer()` will *always* return true because of the limited precision of floating point. – Mark Ransom Oct 13 '20 at 22:06
  • @MarkRansom, can you please tell me how to add large values like `100000000000000000000000000000001 ` as mention in case 2 in my post – learner Oct 13 '20 at 22:28
  • 1
    Either use `int` instead of `float` as suggested by @HampusLarsson, or use the extended precision offered by [`decimal.Decimal`](https://docs.python.org/3.8/library/decimal.html). – Mark Ransom Oct 13 '20 at 22:38
  • Can you [edit] to clarify, because you asked "add floats" and you are casting the inputs to floats, but both of your examples are integers. – Gino Mempin Oct 13 '20 at 23:31
  • @GinoMempin, I added one more example with sample floats now. I added the special cases where my program is failing with sample data and expected outputs – learner Oct 14 '20 at 00:55
  • It seems your question is not really about floats or floating-point precision (to which this was closed as duplicate), but more about string formatting or how to check if inputs are just plain integers, to avoid casting with `float()`. Because your main issue is that you cast everything as floats even if it isn't needed. – Gino Mempin Oct 14 '20 at 08:04
  • Does this answer your question? [Python: return float 1.0 as int 1 but float 1.5 as float 1.5](https://stackoverflow.com/questions/55510485/python-return-float-1-0-as-int-1-but-float-1-5-as-float-1-5) – Tomerikoo Oct 15 '20 at 15:06
  • If this is homework, it could be that they wanted you to do long addition with the strings rather than converting to a numeric type first. – Mark Ransom Oct 15 '20 at 15:48
  • @MarkRansom, can you please tell me what it means long addition and how to do that? – learner Oct 15 '20 at 19:09
  • I mean addition the way you learned as a kid, digit by digit. – Mark Ransom Oct 16 '20 at 02:23

0 Answers0