gpt4 book ai didi

java - double -> 提取整数

转载 作者:行者123 更新时间:2023-12-02 00:14:32 25 4
gpt4 key购买 nike

int a = 4;
int b = 3;
int c = 10;
int d1 =(int) (double)(a*b)/c;
double d2 =(double)(a*b)/c;
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);

Result: d1: 1 and d2: 1.2

如何提取/删除 1.2 中的 1.0。所以我得到 d2 = 0.2 和 d1 = 1 并且 当 a = 9 -> (9*3)/10 时。 d2 = 0.7 且 d1 = 2 并且当 a = 6 -> (6*3)/10。 d2 = 0.8 且 d1 = 1

非常感谢。

最佳答案

int a = 4;
int b = 3;
int c = 10;
// Store the original value.
double original = (double)(a*b)/c;
int d1 = (int)(original);
// Get the difference between the original value and the floored one.
double d2 = original - d1;
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);

关于java - double -> 提取整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12108036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com