比特币 Java RPC 客户端 – 无法创建原始交易

huangapple go评论60阅读模式
英文:

Bitcoin Java RPC client - cannot create raw transaction

问题

我正在使用Java实现的比特币RPC客户端。
当我调用带有int类型的createRawTransaction函数时,生成的原始交易与预期相符:

BitcoindRpcClient.TxOutput txOut1 = new BitcoindRpcClient.BasicTxOutput(issuerAddress,
					new BigDecimal(1));

当我尝试使用double值代替int时:

BitcoindRpcClient.TxOutput txOut1 = new BitcoindRpcClient.BasicTxOutput(issuerAddress,
					new BigDecimal(1.2));

我收到错误信息:无效的金额

当我尝试使用bitcoin-cli进行相同操作时,结果与预期一致。

注意:我正在本地测试网络区块链上操作。

英文:

I'm using the java implementation of Bitcoin RPC client.
When I'm calling the createRawTransaction with int type the raw transaction created as expected:

BitcoindRpcClient.TxOutput txOut1 = new BitcoindRpcClient.BasicTxOutput(issuerAddress,
					new BigDecimal(1));

When I'm trying to use double value instead of int:

BitcoindRpcClient.TxOutput txOut1 = new BitcoindRpcClient.BasicTxOutput(issuerAddress,
					new BigDecimal(1.2));

I'm receiving this error: invalid amount.

When I'm trying it by using bitcoin-cli, it works as expected.

NOTE: I;m working on local testnet blockchain

答案1

得分: 1

以下是翻译好的内容:

输出为:

System.out.println(new BigDecimal(1.2));
System.out.println(BigDecimal.valueOf(1.2));

是:

1.1999999999999999555910790149937383830547332763671875
1.2

因此,简短的答案是使用首选的方法将双精度浮点数转换为BigDecimal.valueOf(1.2)

长答案是浮点数很复杂,double是对1.2的近似。

英文:

The output of:

System.out.println(new BigDecimal(1.2));
System.out.println(BigDecimal.valueOf(1.2));

Is:

1.1999999999999999555910790149937383830547332763671875
1.2

So the short answer is to use the preferred way to convert a double: BigDecimal.valueOf(1.2)

The long answer is that floatting numbers are complicated and double is an approximation for 1.2

huangapple
  • 本文由 发表于 2020年9月15日 22:24:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63903970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定