在转换浮点数期间可能会损失精度(Possible loss of precision during conversion of float number)
我有谷歌如何获得java中的浮点数的2位小数。 以下是我的代码。 最后这里
float totalWeight = 0.1*levinWeight+0.8*lsmWeight;
我得到可能失去精度的错误? 我想首先将字符串转换为float,然后将其设置为2位小数。float levinWeight = Float.parseFloat(dataOnlyCombine[2]); float lsmWeight = Float.parseFloat(dataOnlyCombine[3]); DecimalFormat df = new DecimalFormat("#.##"); levinWeight = Float.valueOf(df.format(levinWeight)); lsmWeight = Float.valueOf(df.format(lsmWeight)); float totalWeight = 0.1*levinWeight+0.8*lsmWeight;
I have google on how to get 2 decimal for a float number in java. Below are my codes. Finally here
float totalWeight = 0.1*levinWeight+0.8*lsmWeight;
I get the error of possible loss of precision ? I would want to first covert the string into float and then have it to be 2 decimal places.float levinWeight = Float.parseFloat(dataOnlyCombine[2]); float lsmWeight = Float.parseFloat(dataOnlyCombine[3]); DecimalFormat df = new DecimalFormat("#.##"); levinWeight = Float.valueOf(df.format(levinWeight)); lsmWeight = Float.valueOf(df.format(lsmWeight)); float totalWeight = 0.1*levinWeight+0.8*lsmWeight;
原文:https://stackoverflow.com/questions/20593682
更新时间:2021-08-10 12:08
最满意答案
您的结果表不够详细,但您可以尝试这样做:
select sales.id, sales.refid, reference.refname, sales.invoice, sales.itemid, sales.price as sellprice, purchase.price as buyprice, sales.price - purchase.price as profit from sales left outer join reference on reference.refid = sales.refid left outer join purchase on purchase.itemid = sales.itemid
Your result table is not detail enough but you can try this :
select sales.id, sales.refid, reference.refname, sales.invoice, sales.itemid, sales.price as sellprice, purchase.price as buyprice, sales.price - purchase.price as profit from sales left outer join reference on reference.refid = sales.refid left outer join purchase on purchase.itemid = sales.itemid
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
虽然这是一个非常广泛的问题,并且您没有提供任何PHP代码,但您可能希望将其分解为各个部分: 建立与数据库的连接(使用mysqli)并发送查询: $c = mysqli_connect("localhost","user","password","db"); if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error(); else { $result = mysqli_query ...
-
如果我正确地读取了您的逻辑,那么我们可以通过在用户与您的用户ID匹配的条件下首先将files加入users来实现它。 然后,我们可以再次向users ,这次是在文件记录是默认的,网络ID匹配,匹配的用户也是您的用户的情况下。 SELECT f.* FROM files f LEFT JOIN users u1 ON f.userID = u1.userID AND f.userID = $userID LEFT JOIN usernetworks u2 ON f.default = 1 AN ...
-
我想你正在寻找UNION条款,一个 (SELECT * from us_music where `genre` = 'punk') UNION (SELECT * from de_music where `genre` = 'punk') I think you're looking for the UNION clause, a la (SELECT * from us_music where `genre` = 'punk') UNION (SELECT * from de_music where ` ...
-
MySQL格式化结果基于表名和多个表数据(MySQL formatting results based on table name and multiple table data)[2024-01-01]
你可以这样写一个查询: select first_name,email,prod_id,group_concat(product_table) as product_table from ( select u.user_id ,first_name,email,prod_id, 'one' as product_table from users u join products_one p on u.user_id = p.user_id where prod_ship_date = '2016-01-01 ... -
有我的建议算法 $mysqli = new mysqli("HOST", "USER NAME", "USER PASS", "YOUR DATABASE"); $result = $mysqli->query("SELECT * FROM table1"); while ($row = $result->fetch_assoc()){ $TID = $row['TID']; $Name = $row['Name']; $result2 = $mysqli->query("SELECT * FROM tab ...
-
您需要为每个表使用DELETE语句。 如果您担心一致性,您可以使用交易。 BEGIN TRANSACTION; DELETE FROM table1 WHERE id = 1; DELETE FROM table2 WHERE id = 1; DELETE FROM table3 WHERE id = 1; DELETE FROM table4 WHERE id = 1; DELETE FROM table5 WHERE id = 1; COMMIT; 使用PDO的 PHP示例(未测试) $id = (i ...
-
我认为你构建评论表的方式有点小问题。 您目前拥有它的方式,客户每个公园只能有一个评论。 如果客户两次访问同一部分,则不能留下第二条评论。 相反,您可能希望评论引用行程编号。 还有一些其他事情并非100%明确您的问题。 例如,我假设你每次旅行需要1行,而不是每个客户一行,但你的问题是你想要“旅行”(复数)。 此外,还不清楚评论是强制性的还是可选的,如果你想在没有评论的情况下看到一行旅行。 假设您对评论表进行了建议的更改,并假设评论是可选的,并且即使没有评论也希望看到行程,我认为这样做可以解决问题: SELEC ...
-
停止mysql服务,在/etc ( /etc/my.cnf mysql目录)中找到文件my.cnf ,打开它,找到可能设置为1MB的变量max_allowed_packet ,根据需要进行更改。 而且,而不是这个: UPDATE table_list A, table_items B SET A.ItemName = B.ItemID WHERE A.ItemName = B.ItemName 尝试这个: UPDATE table_list A SET ItemName = (SELECT ItemID F ...
-
您的结果表不够详细,但您可以尝试这样做: select sales.id, sales.refid, reference.refname, sales.invoice, sales.itemid, sales.price as sellprice, purchase.price as buyprice, sales.price - purchase.price as profit from sales left outer join reference on reference.refid = sales. ...