Hai All
I am ask to do the rounding the Double value
Value =16.415; - public static double roundoff(double value,int roundingFactor){
-
double roundOffValue;
-
BigDecimal bigdecimal = new BigDecimal(value);
-
bigdecimal = bigdecimal.setScale(roundingFactor, bigdecimal.ROUND_HALF_EVEN);
-
roundOffValue = bigdecimal.doubleValue();
-
return roundOffValue;
-
}
-
main method
-
-----------------
-
System.out.println("1->"+EChainNumberFormat.roundoff(16.415,2));
-
System.out.println("2->"+roundoff(16.425,2));
-
System.out.println("3->"+roundoff(16.435,2));
-
System.out.println("4->"+roundoff(16.445,2));
-
System.out.println("5->"+roundoff(16.455,2));
-
System.out.println("6->"+.roundoff(16.465,2));
-
System.out.println("7->"+roundoff(16.475,2));
-
System.out.println("8->"+roundoff(16.485,2));
-
System.out.println("9->"+roundoff(16.495,2));
-
-
-
{
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
10 3706
Perhaps you wanted to use BigDecimal.ROUND_HALF_UP instead?
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
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.
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.
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
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.
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
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?
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 -
public static double roundoff(double value,int roundingFactor)
-
{
-
double roundOffValue;
-
BigDecimal bigdecimal = new BigDecimal(String.valueOf(value));
-
bigdecimal = bigdecimal.setScale(roundingFactor,BigDecimal.ROUND_HALF_UP);
-
roundOffValue = bigdecimal.doubleValue();
-
return roundOffValue;
-
}
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
You could test it yourself and see what happens.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John Cho |
last post by:
// CHO, JOHN
#include<iostream>
class fracpri{
int whole;
int numer;
int denom;
|
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...
|
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
{
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
|
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...
| |