bigdecimal

How to get Java scanner to acknowledge blank input?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 05:18:49
问题 I am having trouble getting my program to respond to empty inputs. For example, say I wanted to prompt the user to enter a value for money with type BigDecimal as well as a currency type. Here is what the program in question looks like. public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the amount of money " + "and specify currency (USD or CNY): "); // initialize variable moneyInput BigDecimal moneyInput; // check if moneyInput is numeric

How using BigDecimal would affect application performance?

让人想犯罪 __ 提交于 2019-11-30 04:59:56
I want to use BigDecimal to represent arbitrary precision numbers like prices and amounts in a low-latency trading application with thousands of orders and execution reports per second. I won't be doing many math operations on them, so the question is not about performance of the BigDecimal per se, but rather about how large volumes of BigDecimal objects would affect performance of the application. My concern is that huge amount of short-lived BigDecimal objects will put a strain on a GC and result in larger Stop-The-World pauses in CMS collector - and this is definitely what I would like to

Formatting a String to Remove Scientific Notation - Java

我与影子孤独终老i 提交于 2019-11-30 04:31:28
问题 I have the following code which splits a string of numbers (separated by a space) and then creates an array of floats : //Split the string and then build a float array with the values. String[] tabOfFloatString = value.split(" "); int length = tabOfFloatString.length; System.out.println("Length of float string is" + length); float[] floatsArray = new float[length]; for (int l=0; l<length; l++) { float res = Float.parseFloat(tabOfFloatString[l]); System.out.println("Float is " + res);

Convert Java Number to BigDecimal : best way

感情迁移 提交于 2019-11-30 04:10:42
I am looking for the best way to convert a Number to a BigDecimal. Is this good enough? Number number; BigDecimal big = new BigDecimal(number.toString()); Can we lose precision with the toString() method ? This is fine, remember that using the constructor of BigDecimal to declare a value can be dangerous when it's not of type String. Consider the below... BigDecimal valDouble = new BigDecimal(0.35); System.out.println(valDouble); This will not print 0.35, it will infact be... 0.34999999999999997779553950749686919152736663818359375 I'd say your solution is probably the safest because of that.

如果需要精确的答案,请避免使用float 和 double(48)

不羁岁月 提交于 2019-11-30 02:57:33
1、float 和 double 主要是为了科学计算和工程计算而设计的 为了得到广泛的数值范围内快速的精确近似值 尤其不适合货币计算 因为 float 和 double 精确的表示0.1是不可能的 BigDecimal 代替double 缺点是很不方便、很慢 你可以完全控制舍入,公有8 种舍入模式供你来选 也可以使用int 和long 来计算,但是以分为单位,而不是元 9位十进制以内用 int 18位以内用long 可能超过18位,只能用BigDecimal 来源: oschina 链接: https://my.oschina.net/u/3847203/blog/1862539

BigDecimal的使用

丶灬走出姿态 提交于 2019-11-30 02:56:50
这次在APP开发中我遇到了精确计算问题.比如服务器给我一个百分比,我根据这个比例计算出价格,送到服务器.比如:2100.05 * 0.2计算的结果并不是420.01,而是420.010000000005.在《Java解惑》这本书中有2.00 - 1.10不是0.9而是0.899999999999999.原因是1.1 这个数字不能被精确表示成为一个double,因此它被表示成为最接近它的double值。二进制浮点对于货币计算是非常不适合的,因为它不可能将0.1——或者10的其它任何次负幂——精确表示为一个长度有限的二进制小数.也就是计算机不认识十分之一,就像我们10进制不认识三分之一一样.书中给出了其中的一些解决方法,比如尽量将将单位缩小,将”元”换成分,避免有float或者double类型的计算.可是正如我碰到的这个问题是没有办法避免的(关键是需求经理不让改),那就只能使用BigDecimal来进行计算.下面就具体说一说BigDecimal的使用方式. BigDecimal进行四则运算 使用BigDecimal,首先就需要构造一个BigDecimal. 比如我们计算2.00 + 1.10 BigDecimal bstr1 = new BigDecimal(“2.00”); BigDecimal bstr2 = new BigDecimal(“1.10”); BigDecimal

Is Java's BigDecimal the closest data type corresponding to C#'s Decimal?

你离开我真会死。 提交于 2019-11-30 02:09:31
问题 According to the chart here, the equivalent data type in Java to C#'s Decimal is BigDecimal . Is this really so? What's up with the "Big" preamble? There doesn't seem to be a "SmallDecimal" or "LittleDecimal" (let alone "MediumSizedDecimal") in Java. I must say, though, that chart was the clearest thing I found on the subject; the other links here and here and here were about as clear to me as the Mississippi River after a torrential tempest. 回答1: Yep - that's the corresponding type. Since

BigDecimal compareTo not working as expected

夙愿已清 提交于 2019-11-30 01:26:45
问题 According to the JavaDoc for BigDecimal , the compareTo function does not account for the scale during comparison. Now I have a test case that looks something like this: BigDecimal result = callSomeService(foo); assertTrue(result.compareTo(new BigDecimal(0.7)) == 0); //this does not work assertTrue(result.equals(new BigDecimal(0.7).setScale(10, BigDecimal.ROUND_HALF_UP))); //this works The value I'm expecting the function to return is 0.7 and has a scale of 10. Printing the value shows me the

Why is BigDecimal returning a weird value?

冷暖自知 提交于 2019-11-30 01:08:30
问题 I am writing code that will deal with currencies, charges, etc. I am going to use the BigDecimal class for math and storage, but we ran into something weird with it. This statement: 1876.8 == BigDecimal('1876.8') returns false. If I run those values through a formatting string "%.13f" I get: "%.20f" % 1876.8 => 1876.8000000000000 "%.20f" % BigDecimal('1876.8') => 1876.8000000000002 Note the extra 2 from the BigDecimal at the last decimal place. I thought BigDecimal was supposed to counter the

BigDecimalUtils BigDecimal加减乘除

拥有回忆 提交于 2019-11-30 00:34:32
public class BigDecimalUtil { private static int DEF_DIV_SCALE = 10 ; // 默认精确的小数位 /** * 提供精确的加法运算。 * * @param v1 被加数 * @param v2 加数 * @return 两个参数的和 */ public static double add ( double v1 , double v2 ) { BigDecimal b1 = new BigDecimal ( Double . toString ( v1 ) ) ; BigDecimal b2 = new BigDecimal ( Double . toString ( v2 ) ) ; return b1 . add ( b2 ) . doubleValue ( ) ; } /** * 提供精确的减法运算。 * * @param v1 被减数 * @param v2 减数 * @return 两个参数的差 */ public static double sub ( double v1 , double v2 ) { BigDecimal b1 = new BigDecimal ( Double . toString ( v1 ) ) ; BigDecimal b2 = new BigDecimal ( Double .