python一箭穿心代码怎样复制,Python Decimal copy_sign()用法及代码示例

Decimal#copy_sign():copy_sign()是一个Decimal类方法,它返回第一个Decimal值的副本,其符号设置为与第二个Decimal值的符号相同。

用法:

Decimal.copy_sign()

参数:

Decimal values

返回:

the copy of the first Decimal value with the sign set

to be the same as the sign of the second Decimal value.

代码#1:copy_sign()方法示例

# Python Program explaining

# copy_sign() method

# loading decimal library

from decimal import *

# Initializing a decimal value

a = Decimal(-1)

b = Decimal('0.142857')

# printing Decimal values

print ("Decimal value a:", a)

print ("Decimal value b:", b)

# Using Decimal.copy_sign() method

print ("\n\nDecimal a with copy_sign() method:", a.copy_sign(a))

print ("Decimal a with copy_sign() method:", a.copy_sign(b))

print ("Decimal b with copy_sign() method:", b.copy_sign(a))

输出:

Decimal value a: -1

Decimal value b: 0.142857

Decimal a with copy_sign() method: -1

Decimal a with copy_sign() method: 1

Decimal b with copy_sign() method: -0.142857

代码2:copy_sign()方法示例

# Python Program explaining

# copy_sign() method

# loading decimal library

from decimal import *

# Initializing a decimal value

a = Decimal('-3.14')

b = Decimal('321e + 5')

# printing Decimal values

print ("Decimal value a:", a)

print ("Decimal value b:", b)

# Using Decimal.copy_sign() method

print ("\n\nDecimal a with copy_sign() method:", a.copy_sign(a))

print ("Decimal a with copy_sign() method:", a.copy_sign(b))

print ("Decimal b with copy_sign() method:", b.copy_sign(a))

输出:

Decimal value a: -3.14

Decimal value b: 3.21E+7

Decimal a with copy_sign() method: -3.14

Decimal a with copy_sign() method: 3.14

Decimal b with copy_sign() method: -3.21E+7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值