bigdecimal

BigDecimal as String in Swagger defenition

核能气质少年 提交于 2019-12-11 06:39:38
问题 I would like to define an object with BigDecimal as String in swagger.json Right now I can do "MyObject": { "type": "object", "properties": { "amountOfMoney": { "type": "string", "pattern": "d+([.]d+)?" }, "name": { "type": "string" } }, "title": "MyObject" } Another way, I can Define amountOfMoney as a number. "MyObject": { "type": "object", "properties": { "amountOfMoney": { "type": "number" "minimum": 0.0, "maximum": 100.0, "exclusiveMinimum": false, "exclusiveMaximum": false }, "name": {

Analog of StringBuilder for BigDecimal

我的梦境 提交于 2019-12-11 04:25:05
问题 I've got a list of BigDecimals to sum. If they were Strings to concatenate, I would use StringBuilder to reduce object creation. Is there something similar for BigDecimal? Or maybe I shouldn't bother about it? Is optimization of BigDecimal creation worth putting effort to it? BigDecimal result = BigDecimal.ZERO; for (CashReportElement element : getReportElementSet()) { if (element.getCurrencyCode().equals(currencyCode)) { result = result.add(element.getSum()); } } return result; 回答1: The is

BigDecimal scale not working

半城伤御伤魂 提交于 2019-12-11 03:15:38
问题 I have the following code: BigDecimal result = BigDecimal.ZERO; result.setScale(2, BigDecimal.ROUND_FLOOR); //1 BigDecimal amountSum; // amount sum computation BigDecimal amountByCurrency = amountSum.divide(32); //-0.04 result.add(amountByCurrency); //2 After line //1 scale is still 0. Why? So, the //2 evaluation doesn't affect to the result. What's wrong? 回答1: The important part of the #setScale documentation is this: Note that since BigDecimal objects are immutable , calls of this method do

BigDecimal in JSTL, divide by int or double returns integer

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 03:11:45
问题 <fmt:formatNumber var="instAmount" value="${invoice.amount / offer.getTotalInstallments()}" minFractionDigits="2" /> Where amount is BigDecimal in Java and totalInstallments is int . I tried by setting totalInstallments to double but nothing changes. It returns an Integer , it behaves as when you divide two integers in Java, you get an integer. Am I missing something or is there a workaround? 回答1: The easiest solution is to just do this calculation in the controller/servlet and use the result

double value 0.0001 will converto 1.0E-4 while inserting into database

穿精又带淫゛_ 提交于 2019-12-11 02:38:50
问题 I would like to pass the double value received from web page to sqlServer database. the value are wrapped in a value object. public class testVO{ Double value; public Double getValue() { return value; } public void setValue(Double value) { this.value = value; } } However, the problem I've met is when the value is smaller than 0.0001, it will transfer into 1.0E-4 or other scientific-notation, which causes the database error. The way I found is to use BigDecimal.valueOf(testVO.getValue()) to

Java precise calculations - options to use

杀马特。学长 韩版系。学妹 提交于 2019-12-11 00:35:49
问题 I am trying to establish some concise overview of what options for precise caluclations we have in JAVA+SQL. So far I have found following options: use doubles accepting their drawbacks, no go. use BigDecimals using them in complicated formulas is problematic for me use String.format/Decimal.format to round doubles do i need to round each variable in formula or just result to get BigDecimal precision? how can this be tweaked? use computed fields option in SQL. drawback is that I'd need

How to insert into SQL Server decimal column?

拜拜、爱过 提交于 2019-12-10 17:44:32
问题 I'm using the MS JDBC driver for SQL Server, and trying to insert values into a decimal(18,5) column. The docs say that decimal columns map to BigDecimal, so I'm trying to do this: PreparedStatement ps = conn.prepareStatement("INSERT INTO [dbo].[AllTypesTable] ([decimal18_0Col]) VALUES (?)"); ps.setObject(1, new BigDecimal(3.14)); ps.execute(); On the call to execute(), I get this error: com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type nvarchar to decimal. The

How can I compare BigDecimals to make my tests pass? [duplicate]

拜拜、爱过 提交于 2019-12-10 15:36:28
问题 This question already has answers here : BigDecimal equals() versus compareTo() (3 answers) Closed 3 years ago . I have the following same strange situation into a JUnit test. So I have this test method: @Test public void getNavInfoTest() throws ParseException { TirAliquotaRamoI expectedObject = new TirAliquotaRamoI(); DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date date; date = formatter.parse("2015-08-01"); Date dataInizio = formatter.parse("2015-08-01"); Date dataFine =

归约

不羁岁月 提交于 2019-12-10 14:18:21
为了防止精度损失,禁止使用构造方法 BigDecimal(double)的方式把 double 值转 化为 BigDecimal 对象。 说明:BigDecimal(double)存在精度损失风险,在精确计算或值比较的场景中可能会导致业务逻辑异常。 如: BigDecimal g = new BigDecimal(0.1f); 实际的存储值为:0.10000000149 正例:优先推荐入参为 String 的构造方法,或使用 BigDecimal 的 valueOf 方法,此方法内部其实执行了 Double 的 toString,而 Double 的 toString 按 double 的实际能表达的精度对尾数进行了截断。 BigDecimal recommend1 = new BigDecimal("0.1"); BigDecimal recommend2 = BigDecimal.valueOf(0.1); 来源: oschina 链接: https://my.oschina.net/u/2552286/blog/3140635

BigDecimal类型比较大小

烈酒焚心 提交于 2019-12-10 14:08:20
有两种方法: 1:转成int型: BigDecimal aa = new BigDecimal ( "1.234" ) ; if ( aa . intValue ( ) > 1 ) { aa = 1 ; System . out . println ( aa ) ; } 2:compareTo方法比较 -1:小于;0:等于;1:大于 BigDecimal aa = new BigDecimal ( "1.234" ) ; if ( aa . compareTo ( BigDecimal . valueOf ( 1 ) ) == 0 ) { aa = 1 ; System . out . println ( aa ) ; } 来源: CSDN 作者: java小童鞋 链接: https://blog.csdn.net/qq_37555514/article/details/103473669