473,322 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

i am not get the correct annswer using big decimal

karthickkuchanur
156 100+
Hai All

I am ask to do the rounding the Double value

Value =16.415;

Expand|Select|Wrap|Line Numbers
  1. public static double roundoff(double value,int roundingFactor){
  2. double roundOffValue;
  3.         BigDecimal bigdecimal = new BigDecimal(value);
  4.         bigdecimal = bigdecimal.setScale(roundingFactor, bigdecimal.ROUND_HALF_EVEN);
  5.         roundOffValue = bigdecimal.doubleValue();
  6.         return roundOffValue;
  7. }
  8. main method
  9. -----------------
  10. System.out.println("1->"+EChainNumberFormat.roundoff(16.415,2));
  11.     System.out.println("2->"+roundoff(16.425,2));
  12.     System.out.println("3->"+roundoff(16.435,2));
  13.     System.out.println("4->"+roundoff(16.445,2));
  14.     System.out.println("5->"+roundoff(16.455,2));
  15.     System.out.println("6->"+.roundoff(16.465,2));
  16.     System.out.println("7->"+roundoff(16.475,2));
  17.     System.out.println("8->"+roundoff(16.485,2));
  18.     System.out.println("9->"+roundoff(16.495,2));
  19.  
  20.  
  21.     {

OutPut
------------
1->16.41
2->16.43
3->16.43
4->16.45
5->16.45
6->16.46
7->16.48
8->16.48
9->16.5

See in 1-> the value is 16.41-when i pass 16.15 i expect 16.42;
but in 2-> the value is 16.425-the output is correct like 16.43

Please give me the solution to round off the value correctly.i user all round fields in bigdecimal like down up

IF the Value 16.425 i want output 16.43 it is correct logic of rouding
Oct 30 '08 #1
10 3706
r035198x
13,262 8TB
Perhaps you wanted to use BigDecimal.ROUND_HALF_UP instead?
Oct 30 '08 #2
karthickkuchanur
156 100+
Perhaps you wanted to use BigDecimal.ROUND_HALF_UP instead?
for ROUND_HALF_UP
1->16.41
2->16.43
3->16.43
4->16.45
5->16.45
6->16.46
7->16.48
8->16.48
9->16.5


For Above input i tested in all cases
Oct 30 '08 #3
r035198x
13,262 8TB
The problem is the constructor you used for BigDecimal.
Use the String one i.e new BigDecimal("" + value);
Read the API specs for both constructors to find out the difference.
Oct 30 '08 #4
JosAH
11,448 Expert 8TB
Those numbers can not be represented as a finite binary expansion so they're
not what you think they are. In the first article of this section, the "Read This First"
article there's a link to a nice paper that describes the deficiencies of the IEEE 754
number system in detail.

kind regards,

Jos

ps. for a quick test: keep on multiplying those numbers by 2 and see if the result
will ever be an integer value.
Oct 30 '08 #5
karthickkuchanur
156 100+
The problem is the constructor you used for BigDecimal.
Use the String one i.e new BigDecimal("" + value);
Read the API specs for both constructors to find out the difference.
Plese tell me how to pass the arugument for my input
Oct 31 '08 #6
r035198x
13,262 8TB
Plese tell me how to pass the arugument for my input
But I've already showed you how in the message you quoted above. Also read the specs for those constructors like I said above.
Oct 31 '08 #7
karthickkuchanur
156 100+
But I've already showed you how in the message you quoted above. Also read the specs for those constructors like I said above.
Yes i have pass the value as string it gor correct answer but if i do like that ,i have to made the changes above 1000 files ,because they pass arugument as double .thanks
Oct 31 '08 #8
r035198x
13,262 8TB
Yes i have pass the value as string it gor correct answer but if i do like that ,i have to made the changes above 1000 files ,because they pass arugument as double .thanks
Make the change only in your roundoff function like I did above. You have only one roundoff function right?
Oct 31 '08 #9
karthickkuchanur
156 100+
Make the change only in your roundoff function like I did above. You have only one roundoff function right?
Sorry iam asking stupid please see my code
Expand|Select|Wrap|Line Numbers
  1. public static double roundoff(double value,int roundingFactor)
  2.     {
  3. double roundOffValue;
  4.         BigDecimal bigdecimal = new BigDecimal(String.valueOf(value));
  5.         bigdecimal = bigdecimal.setScale(roundingFactor,BigDecimal.ROUND_HALF_UP);
  6.         roundOffValue = bigdecimal.doubleValue();
  7.         return roundOffValue;
  8. }
Here in constructor iam type casting double arugument to string
whether iam right ,whether it will affect any,because iam working for the ERP product than why
Oct 31 '08 #10
r035198x
13,262 8TB
You could test it yourself and see what happens.
Oct 31 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
6
by: Mike Conklin | last post by:
This one really has me going. Probably something silly. I'm using dcount for a report to determine the number of different types of tests proctored in a semester. My report is based on a...
3
by: Mahmood Ahmad | last post by:
Hi, I am getting the error 'Input String was not in correct format' in the following C# program (indicated line): using System; namespace CF7 { class CalcArea {
0
by: lianfe_ravago | last post by:
Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the...
17
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then...
3
by: vinnie | last post by:
I have made my first class wih function, trying to calculate the interest paied on a principal. It works until reach the endo f section 2. Section 3 that supposed to print out the results, it does...
4
by: Jeff | last post by:
Hey ..NET 2.0 I'm parsing an XML file and read an attribute value out of a node. This value ( value="0.0000") am I trying to convert to a decimal: decimal _value =...
19
by: Terry Reedy | last post by:
"Luis Zarrabeitia" <kyrie@uh.cuwrote in message news:200805081914.06459.kyrie@uh.cu... | Btw, there seems to be a math problem in python with exponentiation... | >>0**0 | 1 | That 0^0 should be...
9
muaddubby
by: muaddubby | last post by:
Hi I'm running into a scenario with a DataSet that has a schema loaded into it, tries to then load data (that matches the schema), and fails with a "Input string was not in a correct format"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.