我试图使用longs为自己的货币制作自己的类,但显然我应该使用BigDecimal.有人可以帮助我开始吗?什么是用最好的方式BigDecimalS代表美元货币,比如至少使它但没有对仙超过2位小数,等等.对于API BigDecimal是巨大的,我不知道要使用哪些方法.此外,BigDecimal具有更好的精度,但如果它通过一个double?如果我做新的BigDecimal(24.99),它会如何与使用double?或者我应该使用使用的构造函数String?
我写了一个类来测试相等,小于和大于Java中的两个双重.我的一般情况是比较可以具有半分精度的价格.59.005比59.395.我选择的epsilon适合这些情况吗?
private final static double EPSILON = 0.00001;
/**
* Returns true if two doubles are considered equal. Tests if the absolute
* difference between two doubles has a difference less then .00001. This
* should be fine when comparing prices, because prices have a precision of
* .001.
*
* @param a double to compare.
* @param b double to compare.
* @return true true if two doubles are considered equal.
*/
public static boolean equals(double a, double b){ …Run Code Online (Sandbox Code Playgroud) 下面列出的场景的最佳做法是什么?
我们有一个应用程序,我们想支持多种货币.该软件将尊重用户的区域设置和区域设置,以指示正确的数字格式,即$ 10,000.00或10.000,00₴等.
但是,我们希望能够根据货币ID(可能是三个字母的ISO4217代码)格式化不同的数字.我们的想法是使用每种货币在数据库中存储一个表,然后请求用户选择将使用的货币符号.然后,程序将根据区域设置/区域设置格式化所有数字,但将使用我们程序中定义的货币符号.
因此,给出以下数值和货币
10000 AUD
5989.34 USD
450 EUR
该计划将输出
$ 10,000.00
$ 5,989.34
€450.00
或者使用区域设置将数字格式化为#####,## $结果将是;
10000,00 $
5989,34 $
450,00€
尊重区域设置/区域编号格式但显示不同的货币,最佳做法是什么?
我希望这是有道理的.
使用的技术是c#.NET 2.0.
我无法将长(美分)转换成货币格式.
我的代码:
long doublePayment = 1099; //Should equal $10.99
DecimalFormat dFormat = new DecimalFormat();
String formattedString = dFormat.format(doublePayment);
System.out.println(formattedString);
Run Code Online (Sandbox Code Playgroud)
产量:1,099
我也尝试过:
long doublePayment = 1099;
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
String s = n.format(doublePayment);
System.out.println(s);
Run Code Online (Sandbox Code Playgroud)
由于这是美分,输出应为10.99或10.99美元.
无法弄清楚我做错了什么.谢谢!!!
可能重复:
用Java表示货币价值
哪种java类型处理价格值好?请告诉我为什么?
我不知道这只是问题:)
我只知道这种类型必须"安全".因为如果我们创建商店应用或者那样的想法.
PS.我需要最好的实践
以及它在数据库中的表现方式?
在欧洲,小数用' , ' 分隔,我们使用可选' .'分开成千上万.我允许货币值:
我使用下一个正则表达式(来自RegexBuddy库)来验证输入.我允许可选的两位数分数和可选的千位分隔符.
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{0,2})?|(?:,[0-9]{3})*(?:\.[0-9]{0,2})?|(?:\.[0-9]{3})*(?:,[0-9]{0,2})?)$
Run Code Online (Sandbox Code Playgroud)
我想将货币字符串解析为浮点数.例如
123,456.78应存储为123456.78
123.456,78应存储为123456.78
123.45应存储为123.45
1.234应存储为1234 12.34应存储为12.34
等等...
在Java中有一种简单的方法吗?
public float currencyToFloat(String currency) {
// transform and return as float
}
Run Code Online (Sandbox Code Playgroud)
使用BigDecimal而不是Float
感谢大家的回答.我已将我的代码更改为使用BigDecimal而不是float.我会用浮动来保留这个问题的前一部分,以防止人们犯同样的错误.
解
下一个代码显示了一个函数,它将美国和欧盟货币转换为BigDecimal(String)构造函数接受的字符串.这就是说一个没有千分隔符的字符串和一个分数点.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestUSAndEUCurrency {
public static void main(String[] args) throws Exception {
test("123,456.78","123456.78");
test("123.456,78","123456.78");
test("123.45","123.45");
test("1.234","1234");
test("12","12");
test("12.1","12.1");
test("1.13","1.13");
test("1.1","1.1");
test("1,2","1.2");
test("1","1");
}
public static void test(String value, String expected_output) throws Exception {
String output = currencyToBigDecimalFormat(value);
if(!output.equals(expected_output)) {
System.out.println("ERROR expected: " …Run Code Online (Sandbox Code Playgroud) 使用瑞士法郎算法舍入货币金额时,将考虑第二个和第三个十进制数字.如果小于26,则将它们向下舍入为0; 否则如果小于76,则向下舍入到5; 否则整个价值被四舍五入.
20.125 => 20.10
20.143 => 20.15
20.179 => 20.20
Run Code Online (Sandbox Code Playgroud)
当舍入的量具有更大的小数精度时会发生什么?是否只是忽略了第三个之后的所有十进制数字(值被截断),或者首先以某种其他方式将值舍入为三位小数?例如,考虑截断与"Math.round()"方法(小于0.5舍入,否则向上舍入):
Truncation | "Math.round"
=================================================================
Start 3 d.p. Rounded | Start 3 d.p. Rounded
=================================================================
20.1259 -> 20.125 => 20.10 | 20.1259 -> 20.126 => 20.15
20.1759 -> 20.175 => 20.15 | 20.1759 -> 20.176 => 20.20
Run Code Online (Sandbox Code Playgroud)
如上所示,这些边缘情况在最终结果中变化很大.
阿根廷货币四舍五入遵循类似的模型,只关注第三个十进制数字.虽然舍入结果可能有两位或三位小数,但同样的原则适用; 如果要舍入的值有四个或更多的十进制数字,算法是否应该截断第三个数字之后的任何内容,还是应该应用其他类型的中间舍入来获得第一个三位小数的结果?
谢谢!
我正在开发一个项目,需要对货币执行一些简单的数学运算,但它以String的形式到达.我是Java/Android的新手,所以我正在寻找帮助从String转换为适合此操作的数据类型.起初我认为Float是正确的但是在读完其他地方并将自己介绍给数字类后,看起来BigDecimal是正确的.我是在正确的轨道上吗?此时,我只想从初始发票金额中减去付款总额.我觉得这段代码很简单但很笨拙,我怀疑我对使用货币的细微差别缺少很多.你会怎么做?所有建议都热烈赞赏!
// capture variables from sending activity
String invoiceAmt = getIntent().getStringExtra("invoiceAmt");
String paymentsSum = getIntent().getStringExtra("paymentsSum");
// convert strings to BigD's
BigDecimal amt = new BigDecimal(invoiceAmt);
BigDecimal sum = new BigDecimal(paymentsSum);
// Do the math
BigDecimal invDue = amt.subtract(sum);
// insert the value (back to string) into textView
TextView tvInvoiceDue = (TextView)findViewById(R.id.InvoiceDue);
tvInvoiceDue.setText(invDue.toString());
Run Code Online (Sandbox Code Playgroud) 我有一种方法返回一个double.当测试这个方法作为我的一部分时jUnit,我注意到以下奇怪之处:
String a = "someString";
String b = "someDifferentString";
double result = c.getScore(a, b, true);
System.out.println(result); // prints 0.0
assert (result > 0.0); // Test passes
Run Code Online (Sandbox Code Playgroud)
那么..我问你,0.0怎么能超过0.0?为什么要result > 0.0评估true?
我希望能够关联这两个数组,以便当我从用户输入开始时,我可以访问名称和价格
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) 我有一个数据库order,price其deposit字段设置为浮点类型.我也正在实现java gui来搜索订单,问题是当我尝试在数据库中搜索订单时我找不到任何东西,因为当我从字符串转换为float时它节省了价格= 55.0并且在数据库中它保存为55.问题是什么?我应该使用什么类型来表示来自双方,java方面和mysql方面的货币?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
//collect arguments
String category = this.jTextField8.getText();
String pattern=this.jTextArea1.getText();
String color=this.jTextArea2.getText();
float price = Float.valueOf(this.jTextField14.getText()).floatValue();
float deposit = Float.valueOf(this.jTextField15.getText()).floatValue();
//create new order
int row=this.customerSelectedRow;
Order newOrder=new order(this.customerModel.dataVector.get(row),category,pattern,color,price,deposit);
newOrder.search();
//refresh
this.jDialogNewOrder.setVisible(false);
}catch(Exception e){
System.err.println (" Error message: " + e.getMessage ());
}
}
Run Code Online (Sandbox Code Playgroud)
这是搜索方法的代码
try {
s = c.conn.prepareStatement("SELECT id FROM `"+this.table+
"` WHERE customerId=? AND category=? and pattern=? and color=? and deposit=? and price=?");
s.setInt(1,this.customer.getId());
s.setString …Run Code Online (Sandbox Code Playgroud) java ×10
currency ×7
bigdecimal ×2
double ×2
rounding ×2
types ×2
android ×1
arrays ×1
c# ×1
cultureinfo ×1
junit ×1
long-integer ×1
mysql ×1
precision ×1
regioninfo ×1
string ×1