-1

When I return a Decimal value and print it:

return Decimal(number).quantize(Decimal(str(1 / 10 ** digits)), rounding=ROUND_HALF_UP)

it prints the output within this Decimal('')

Example:

[Decimal('1.20') Decimal('1.22') Decimal('1.42') Decimal('1.75') Decimal('2.77')]

But when I print the value in the function itself it prints only the value.

Example:

1.20
1.20
1.22
1.42
1.75
2.77

Once I exit the function how can I get the numeric value without the it being inside Decimal('')

Thank you.

guroosh
  • 642
  • 6
  • 18
  • You must not be printing it the same way in both cases. The first result is printing the list in one step, the second result looks like you wrote a loop that printed each element separately. – Barmar May 24 '19 at 09:33
  • Inside a list, it will use the `repr`esentation of Decimal, which is different. You could use `[str(x) for x in list_of_decimals]` instead. – 9769953 May 24 '19 at 09:34
  • @9769953 But then it will print them with quotes around them when you print the list. – Barmar May 24 '19 at 09:35
  • Your first and second example aren't the same: the first is a single Decimal, the second a list of Decimals (or almost a list: there are commas missing, so I'm not 100% sure what it is). That makes a difference. – 9769953 May 24 '19 at 09:35
  • @Barmar Yes, but one shouldn't print the list in the first place, and expect nicely formatted results. That's not what a list is for. – 9769953 May 24 '19 at 09:36
  • 1
    Show the code that you're using to print the value in each case. – Barmar May 24 '19 at 09:38

2 Answers2

1

When I return a Decimal value and print it:

return Decimal(number).quantize(Decimal(dstr(1 / 10 ** digits)), rounding=ROUND_HALF_UP)

it prints the output within this Decimal('')

That is not true. You didn't print it. Your interpreter displayed something after returning the value. In fact, it chose to display the string returned by <your_decimal_object>.__repr__()

When you call print, you display the string returned by <your_decimal_object>.__str__()

>>> from decimal import Decimal

>>> def decimal_function(number, digits, ROUND_HALF_UP):
        return Decimal(number).quantize(Decimal(str(1 / 10 ** digits)), rounding=ROUND_HALF_UP)

>>> a = decimal_function(1.0, 2, 'ROUND_HALF_UP')

>>> a

Decimal('1.00') 

>>> print(a)

1.00

>>> repr(a) # same as a.__repr__()

"Decimal('1.00')"

>>> str(a) # same as a.__str__()

'1.00'

Read this to understand the different purposes of repr and str

Corentin Limier
  • 4,946
  • 1
  • 13
  • 24
0

Not sure what the values of number and digits are, but say:

from decimal import *
number = 10
digits = 2

x = Decimal(number).quantize(Decimal(str(1 / 10 ** digits)), rounding=ROUND_HALF_UP)

print(x)
>>> Decimal('10.00')
type(x)
>>> <class 'decimal.Decimal'>
float(x)
>>> 10.0
ron_g
  • 1,474
  • 2
  • 21
  • 39
  • `quantize()` returns a list, you can't convert that to a float. – Barmar May 24 '19 at 09:34
  • This also negates the benefit of using the `Decimal` type, which avoids the inaccuracies of floating point. – Barmar May 24 '19 at 09:36
  • I'm sure it does, but they asked "Once I exit the function how can I get the numeric value without the it being inside Decimal('')". I'm assuming using `Decimal()` within the function is where it's useful, but they want a number to be returned. – ron_g May 24 '19 at 09:42
  • No, they just don't want `Decimal` to be printed. – Barmar May 24 '19 at 09:43
  • `Decimal` values are numbers, they just use fixed-point arithmetic instead of floating point. – Barmar May 24 '19 at 09:44