我有一个简单的计算,使用双打,但我得到一个意想不到的结果,我不明白为什么?
import java.util.Scanner;
public class VersatileSnitSoft {
/**
* @param args
*/
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
double amount;
System.out.print("What’s the price of a CD-ROM? ");
amount = myScanner.nextDouble();
amount = amount + 25.00;
System.out.print("We will bill $");
System.out.print(amount);
System.out.println(" to your credit card.");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我输入2.99,我得到的结果是......
We will bill $27.990000000000002 to your credit card.
Run Code Online (Sandbox Code Playgroud) 我有一个字符串salePrice可以有类似的值29.90000,91.01000,我想得到输出像29.90,91.01小数点后的两位数.我正在使用一个字符串.
我真的不明白指针指针是如何工作的.没有使用指针指针的任何方式做同样的工作?
struct customer
{
char name[20];
char surname[20];
int code;
float money;
};
typedef struct customer customer;
void inserts(customer **tmp)
{
*tmp = (customer *)malloc(sizeof(customer));
puts("Give me a customer name, surname code and money");
scanf("%s %s %d %f", (*tmp)->name, (*tmp)->surname, &(*tmp)->code, &(*tmp)->money);
}
Run Code Online (Sandbox Code Playgroud) 可能的重复:
如何在 Java 中将数字四舍五入到 n 个小数位
在 Java 中将两个数字相乘时会发生这种情况:
double a = 9.495 * 100;
Run Code Online (Sandbox Code Playgroud)
预期结果:
a = 949.5;
Run Code Online (Sandbox Code Playgroud)
但是得到的结果是:
a = 949.4999999999999
Run Code Online (Sandbox Code Playgroud)
当我尝试将数字 9.495 舍入两位小数时,结果是 9.49 而不是 9.50
任何想法如何解决这个问题?
我目前正在编写一个程序,它将读取指定的文本文件,该文件检查每个买入/卖出/摘要的交易值,并检查算术,如果买卖报表中的交易不等于总交易量.在摘要中给出它然后输出错误并关闭程序.但是目前我的方法scanMoneyValue有一个错误,它说它没有返回一个double,实际上它是.我应该采用不同的方式从我的方法中返回值吗?这是我的代码供参考:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class RecurrsionFileChecker {
public static void main(String[] args) {
int result;
//File Chooser Window
JFileChooser chooser = new JFileChooser("/home/nick/workspace/CS 1410-001/src/assignment03");
chooser.setDialogTitle("Please choose a file to be checked");
result = chooser.showOpenDialog(null);
//User Cancelled the chooser
if (result == JFileChooser.CANCEL_OPTION)
return;
File inputfile = chooser.getSelectedFile();
try
{
Scanner in = new Scanner(inputfile);
//Call Method to look at next transaction
scanNextTransaction(in);
}
catch (IOException e)
{
System.out.println("Could not read …Run Code Online (Sandbox Code Playgroud) 我从我的项目中获得了不必要的价格长度.举个例子,如果价格是12.99我会得到的12.9899997711
我的数据库看起来像下面有800多个项目
产品表
ppid name dec
1 shoes black shoes
2 hat red hat
Run Code Online (Sandbox Code Playgroud)
item_product表
我的价格是 type float(5,2)
Item_ID ppid price
1 1 12.99
2 2 10.00
Run Code Online (Sandbox Code Playgroud)
PHP/HTML 这就是我展示价格的方式
<?php
dbconnection();
$stmt2 = $conn->prepare("SELECT name, Price FROM item_product WHERE ppid=:id LIMIT 1");
$stmt2->bindParam('id',$id);
$stmt2->execute();
$rows2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows2 as $row2) {
if ($i == 0) {
echo '<td>Price:</td>';
echo '<td name="pricetag" class="pricetag" id="pricetag">£'.$row2['Price'].'</td>';
}
}
?>
Run Code Online (Sandbox Code Playgroud)
总结
如何才能显示它.99以及最后.00包含任何项目的任何项目.
我希望能够关联这两个数组,以便当我从用户输入开始时,我可以访问名称和价格
public class fastFood
{
public static void main (String[]args)
{
//array for prices and order
double[] prices= new double[]{"5.79", "6.79", "4.59", "5.39", "6.59", "7.29", "6.09", "5.69"};
String[] menu= new String[8];
menu[0]="Whopper Meal";
menu[1]="Double Whopper Meal";
menu[2]="Chipotle Whopper Meal";
menu[3]="Whopper Jr. Meal";
menu[4]="BK Double Stacker Meal";
menu[5]="Premium Chicken Sandwhich (Crispy or Grilled) Meal";
menu[6]="Chipotle Chicken Sandwhich (Crispy or Grilled) Meal";
menu[7]="OriginalChicken Sandwich Meal";
}
}
Run Code Online (Sandbox Code Playgroud) 假设浮点数的十进制可视化表示后的2位数(例如:货币),以最小总误差分割金额的最佳方法是什么?
例如,要将100.00平均分配给3个银行账户,我会做这样的事情:
double amount = 100.0;
double acc1 = amount / 3.0;
double acc2 = amount / 3.0;
double acc3 = amount / 3.0;
Run Code Online (Sandbox Code Playgroud)
但是,当用2位小数打印每个帐户余额时,我得到:
printf("%.2f\n", acc1);
printf("%.2f\n", acc2);
printf("%.2f\n", acc3);
33.33
33.33
33.33
Run Code Online (Sandbox Code Playgroud)
很明显,所有帐户的金额总和为99.99,由于四舍五入而损失0.01.
理想情况下,我想要一些功能/算法可以分布几乎相同和视觉打印
33.34
33.33
33.33
Run Code Online (Sandbox Code Playgroud)
这三个账户中的哪一个获得额外的0.01并不重要.
我该怎么做呢?是否有任何舍入算法名称?
我有一个csv文件,其中包含一些带有美元符号的单元格(例如$46.5)。我强迫所有类型都numpy.float64在函数中pandas.read_csv()。它抱怨ValueError: could not convert string to float: $46.5。有没有办法干净地处理这个问题?
当我输入0.50或其他可以被除以的浮点数时,0.25我不会得到任何错误,但是当我输入0.10或无法被我编写0.25的elseif语句除以的浮点数时,不会发生。谁能告诉我为什么?
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void) {
float change;
do {
change = get_float("Change owed: ");
} while (change < 0.009);
int count = 0;
int div = 0;
while (change > 0.00) {
if ((change / 0.25) >= 1.00) {
div = round(change / 0.25);
count += div;
change = change - (div *0.25);
} else
if ((change / 0.10) >= 1.00) {
div = round(change …Run Code Online (Sandbox Code Playgroud)