在Java中,没有精确表示浮点运算.例如这个java代码:
float a = 1.2;
float b= 3.0;
float c = a * b;
if(c == 3.6){
System.out.println("c is 3.6");
}
else {
System.out.println("c is not 3.6");
}
Run Code Online (Sandbox Code Playgroud)
打印"c不是3.6".
我对超过3位小数的精度感兴趣(#.###).我如何处理这个问题来乘以浮点数并可靠地比较它们?
我正在写一个django模型,允许我的网站有优惠券.
优惠券可以有三种类型:终身帐户凭证,特定月份凭证,一定数量的美元凭证.
为了简单起见,我只允许优惠券拥有三个可能值中的一个(即优惠券不能是10美元和5个月).但我想检查优惠券何时被保存以确保此规则为真.
目前我有:
true_count = 0
if self.months:
true_count += 1
if self.dollars:
true_count += 1
if self.lifetime:
true_count += 1
if true_count > 1:
raise ValueError("Coupon can be valid for only one of: months, lifetime, or dollars")
Run Code Online (Sandbox Code Playgroud)
我知道有更好的方法可以做到这一点,但我没有看到它(称之为编码器的块).
非常感谢帮助.
如果是maters,则三种类型是int,int和bool
months = models.IntegerField(default=0)
cents = models.IntegerField(default=0)
#dollars = models.FloatField(default=0.00)
#dollars replaced with integer cents per advice of group
lifetime = models.BooleanField(default=False)
Run Code Online (Sandbox Code Playgroud) 我正在尝试在格式化文本中做一些简单的输出.Setprecision不会将我的变量打印到两个小数位.
例如,如果firstItemPrice = 2.20,则输出为2.2而不是2.20
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string firstitem = "";
string seconditem = "";
double firstItemNum;
double firstItemPrice = 0.00;
double secondItemNum;
double secondItemPrice = 0.00;
//first item
cout << "Enter the name of Item 1: ";
getline(cin, firstitem);
cout << "Enter the number of " << firstitem << "s and the price of each: ";
cin >> firstItemNum >> firstItemPrice;
cin.ignore();
//second item
cout << "Enter the name of Item 2: …Run Code Online (Sandbox Code Playgroud) 我正在制作一个模拟银行交易的程序.我必须询问用户是否要存款,取款或转账.
当我存入一定金额(例如1000)时,它表示我的余额是1000.然后我要求提取400这样的数字,它表示我的余额为-400.毕竟我想也许我必须检查我的平衡然后它会给我正确的平衡应该是600,但它说0.例如,看到这个记录:

我在想,因为在我的代码中(如下所示)我使得余额= 0但是如果我把= 0拿走并尝试运行程序它说它需要初始化.
我被卡住了,我想弄清楚.请不要发布整个代码更正.我想自己修理并学习!
import java.util.Scanner;
public class BankTransactions {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num;
do {
double balance = 0;
double amount;
System.out.println("Type Number");
System.out.println("1. Deposit");
System.out.println("2. Withdrawal");
System.out.println("3. Balance");
System.out.println("4. Exit");
num = scan.nextInt();
if (num == 1) {
System.out.println("Enter amount to deposit: ");
amount = scan.nextDouble();
// Add the amount to the balance
balance += amount;
System.out.println("Your balance is");
System.out.println(balance);
} else if (num == 2) { …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
double d = 9000000000000000000d;
while (d == 9000000000000000000d)
{
d += 500;
Console.WriteLine(d);
}
Run Code Online (Sandbox Code Playgroud)
为什么代码会遇到无限循环?
为什么编译器不抛出任何异常?
正如你可以通过互联网阅读使用浮动代表货币是一个非常糟糕的主意.建议的最佳做法是使用代表美分的整数.这样就可以安全地避免遇到任何精度问题,尤其是在进行一些计算时.
因为我天真而且过于乐观,我选择了 - 尽管有所有警告 - 浮动代表我的应用程序中的货币.起初它进展顺利.现在我遇到了各种各样的问题(特别是比较),并希望从浮点数切换到整数.
不幸的是,angular不支持分整数作为货币过滤器的输入(至少据我所知).我有点惊讶,到目前为止看起来没有人提出这个问题(github上没有相应的问题,SO等都没有).
有没有最佳做法?您是否可以想到像这样的简单过滤器可能具有的任何缺点:
angular
.module('myApp')
.filter('cents', ['$filter', function($filter) {
return function(cents, symbol, fractionSize) {
var asFloat = cents / 100;
return $filter('currency')(asFloat, symbol, fractionSize);
};
}]);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 SwiftDecimal结构进行货币操作,但无法对其进行格式化。
如何格式化var myDecimal:Decimal = 9999.99以显示 $9,999.99?
不使用Decimals 我可以按如下方式执行...
let myTotal:NSNumber = 9999.99
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = NSLocale.current
let priceString = currencyFormatter.string(from: myTotal)
myLabel.text = priceString
Run Code Online (Sandbox Code Playgroud)
这很好用,但我一直在阅读,Decimal似乎是正确的货币类型。
我试过...
let myTotal:Decimal = 9999.99
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
// localize to your grouping and decimal separator
currencyFormatter.locale = NSLocale.current
let priceString = currencyFormatter.string(from: NSNumber(myTotal))
myLabel.text = priceString
Run Code Online (Sandbox Code Playgroud)
...但我得到错误
参数标签“(_:)”不匹配任何可用的重载
Decimal …
我有一个有3列的表:
customer_name varchar
,account_type varchar
,current_balance double precision
Run Code Online (Sandbox Code Playgroud)
current_balance的示例值:
1200 1500.5 1500
我希望它们显示如下:
1200.00 1500.50 1500.00
我尝试了以下查询:
SELECT to_char(current_balance,'9999999999999999D99')
FROM bank;
Run Code Online (Sandbox Code Playgroud)
它按我想要的方式格式化,但在开头添加空格.怎么解决这个?有更好的格式化方法吗?
在 MongodDB 中,有一种数据类型“Decimal128”可以正确保存小数值(请参阅此处的“原因” 。
在 firebase 中存储/使用小数和货币类型的推荐方法是什么?与 Bigdecimal 相互转换吗?或者 firestore 中的小数类型足以克服舍入问题吗?
我对头文件中std::get_money定义的C ++函数感到困惑<iomanip>。get_money按编程概念的用途是什么?
我使用以下代码std::get_money。
#include <iostream> // std::cin, std::cout
#include <iomanip> // std::get_money
int main ()
{
long double price;
std::cout << "Please, enter the price: ";
std::cin >> std::get_money(price);
if (std::cin.fail()) std::cout << "Error reading price\n";
else std::cout << "The price entered is: " << price << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我输入100.25时,它返回100。输出和货币格式之间是什么关系?我阅读了此参考资料,但无法理解这种关系。同样的困惑存在与std::put_money,std::get_time,和std::put_time。
实际使用的例子有哪些?
c++ ×2
java ×2
angularjs ×1
c# ×1
currency ×1
decimal ×1
django ×1
formatting ×1
iomanip ×1
javascript ×1
node.js ×1
postgresql ×1
precision ×1
python ×1
swift ×1