Rounding numbers to 2 decimal places
Hello programming community,
I have the following code to round my answers to 2 decimal places:
double roundedValue;
DecimalFormat twoDForm = new DecimalFormat ("#.##");
roundedValue = Double.valueOf(twoDForm.format(value));The code works great and what i expect it to do. When I pass in really big decimal numbers (2.55555668) it correctly rounds it (2.56) but when it rounds the number and the last decimal is a zero it does not display this. (if the number ends up rounding to 4.50 , i want it to display it this way but it ends up getting rid of the 0. )
If anyone has any ideas for a way to fix this please let me know.
Thanks.
the-java-guy-needs-help wrote:
i thought that my equation would be causing a problem and that i might need to do a cast -- I relized that this is not the problem
for the type of string i am using the ("0.00")
After trying various things the string does work if im just outputing it to the screen but i also use this rounding in a method that needs to return a double so i still need to convert it to a double.
I have tried various ways of converting string to double (Double.parseDouble) and i tried one that involved a trim() but both methods drop the zero.You've got a basic misconception still going on here. If you convert it to a double, there is no such thing as a trailing zero -- it's meaningless since it only has meaning for a String representation of a double and not the intrinsic number itself.
Similar Messages
-
Price per unit is getting round off to two decimal places.
Hi Gurus,
In the Billing output, the Price per unit is getting round off to two decimal places.
How can we avoid this or allow 4 decimal places for such cases.
In the same pricing procedure, we have two pricing condition types PR02 and ZR01.
When PR02 is being used,it doesnt rounds off Price per unit.e.g .578888 = .578888
But when ZR01 is being used the system rounds off price per unit.e.g.578888 = .58
Please suggest where is the setting for it.
Thanks
MonteeHi,
Which currency you are using? If its INR, then it will do only for 2 decimal places.
The solution for ur problem can be ractified by changing the scales :
Also check OSS 80183
Thanks,
Raja -
How do I round calculations to two decimal places (form-wide)
Hi,
I've created an order form where customers input the quantity of product they want to order and the form outputs the total due (including shipping and sales tax). Depending on user input, the resulting total sometimes contains more than two decimal places. How do I round calculations to two decimal places? Is there a way to do this form-wide?
I'm new at using Acrobat for calculations. Any help would be greatly appreciated! Thanks...The above will affect the displayed value not the actual value of the field nor its value when accessed in another computation. This behavior may cause an error of 1 cent or more in the grand total or sales tax computation. If you want the the value and displayed value to be the same value you can use the following Validation script:
event.value = util.scand("%,1 0.2f", event.value); -
Round off to two decimal places
hi
i have a filed in it values are there i want to round off to two decimal places
which is the function module for it please suggest
regards
AroraHi Nishant Arora,
Please check this code.
DATA: N1 TYPE P DECIMALS 4 VALUE '0.0565',
N2 TYPE P DECIMALS 2.
MOVE N1 TO N2.
WRITE: N2.
Here the value is get rounded to 0.06.
Rather than function modules you go with this. I think it is easier.
Reward points if useful.
Cheers,
Swamy Kunche -
Rounding off to two decimal places
Hello,
For simplicity, is there a method which will round off
a double variable to two decimal places.
Excample: if I have a result which equals 2.1999998
and I want to display this as 2.20 in a TexTField.
Any help would be much appreciated.
ThanksIf you are trying to do dollars and cents, I further recommend you use:NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
String currencyOut = currencyFormatter.format(yourNumber);to format your numbers as they will give the correct precision for international currencies simply by setting the locale, and they will also automatically handle the currency symbols ($, DM FR, etc.)
Doug -
Displaying all numbers with 2 decimal places
Hi All,
We have a requirement in our code where we want that all numbers must be diplayed with 2 decimal places.
Eg: If a number is 21.234 then it should be displayed as 21.23
If a number is 21.289 then it should be displayed as 21.28
If a number is 21 then it should be displayed as 21.00
Can anyone please help me how to do it in a SQL query.
Regards,
ShrutiHi, Shruti,
This shows a couple of things you can do:
COLUMN trnc FORMAT 999999.99
WITH got_x AS
SELECT empno / 1000 AS x
FROM scott.emp
SELECT x
, TO_CHAR (x, '999999.99') AS tc
, TO_CHAR (TRUNC (x, 2), '999999.99') AS trnc_tc
, TRUNC (x, 2) AS trnc
FROM got_x
;Output:
` X TC TRNC_TC TRNC
7.369 7.37 7.36 7.36
7.499 7.50 7.49 7.49
7.521 7.52 7.52 7.52
7.566 7.57 7.56 7.56
7.654 7.65 7.65 7.65
7.698 7.70 7.69 7.69
7.782 7.78 7.78 7.78
7.788 7.79 7.78 7.78
7.839 7.84 7.83 7.83
7.844 7.84 7.84 7.84
7.876 7.88 7.87 7.87
7.9 7.90 7.90 7.90
7.902 7.90 7.90 7.90
7.934 7.93 7.93 7.93Column x is the number, as SQL*Plus displays it by default. Note there are as many digits after the decimal point as are needed.
Column tc shows how TO_CHAR can format the number, with exactly 2 digits after the decimal point. This automatically rounds the number to the nearest multiple of .01, so on the first row (for example) 7.369 gets displayed as 7.37.
user11272043 wrote:
... If a number is 21.289 then it should be displayed as 21.28You (apparantly) always want the number rounded toward 0, so column tc isn't quite what you want.
Column trnc_tc is exacly what you requested, with the numbr rounded toward 0 (when any rouning is necessry) and 2 digits after the decimal point.
Column tc is also exactly what you requested, but it uses SQL*Plus formatting rather than SQL. Also, column trnc is a NUMBER, while column trnc_tc is a VARCHAR2, because TO_CHAR, as its name hints, returns a VARCHAR2.
The SQL*Plus command
SET NUMFORMAT 999999.99changes the default format for all number columns. If you use this, you can override it for individual columns by using TO_CHAR (since it returns a VARCHAR2, the NUMFORMAT doesn't apply),or a COLUMN command. Also, if you use SET NUMFORMAT 999999.99, you'll still have to explicitly TRUNC every number, or else 21.289 will come out as 21.29, not 21.28. -
Rounding Rule upto 3 Decimal Places in Tax Condition(SABRIX)
Hi,
We are Using SABRIX for Tax determination and need to round up Condition Type XR1- XR6 to round upto 3 Decimal Places.
Do we need to To Change ALT Calc Type- 301306or Alt Cond Base Value -- 16 or have to change Currency decimal Places in transaction OY04/OY03.
Kindly Suggest.
Thanks
AmitHi,
Tax % for customer is decided in SABRIX, Where does TaX calculated?? In SABRIX or in SAP.
Thanks
Amit
Edited by: AMIT GUPTA on Mar 31, 2010 10:54 AM -
Rounding Doubles to Two Decimal Places
Hi All,
I've searched the archive and found a few different posts regarding restricting the number of decimal places in doubles. However they all suggest different methods, BigDecimal, NumberFormat etc.
Which is the simplest method of rounding a number say 10.023445656 to 10.02?
I tried using the java.text.NumberFormat but this turns the double into a string and I need the end result to be a double.
ThanksHi All,
I've searched the archive and found a few different
posts regarding restricting the number of decimal
places in doubles. However they all suggest different
methods, BigDecimal, NumberFormat etc.
Which is the simplest method of rounding a number say
10.023445656 to 10.02?
I tried using the java.text.NumberFormat but this
turns the double into a string and I need the end
result to be a double.
ThanksI ahve a small code that can do the work for u:
import java.text.*;
double format(double val, int dec){
double multiple=Math.pow(10,dec);
val=Math.round(val*multiple)/multiple;
DecimalFormat df=new DecimalFormat("0.00");
String format=df.format(val);
double dval=Double.parseDouble(format);
return dval;
}//end of functionHope that helps! -
Rounding off to 2 decimal places
Hi,
I use "Double" for my calculations and since I am working with $$ ;-) I need to round it off at 2 decimal places. Any quick way to do this? or do I have to write some major code for that?
ThanksThis works for all the test cases. Try this
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
class RoundOff {
public static void main(String args[]) {
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("173449.8"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("173449.98"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("-1.0"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("-1.0"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("-141.036"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("-00.1"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("-0.0"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("173449.98"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("173449.0.54"));
//RARE EXCEPTIONS BUT WONT OCCUR
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("0125."));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("0125.979.79E"));
//RARE EXCEPTIONS BUT WONT OCCUR
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("173449.2354"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("173449.9874"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("173449.999"));
System.err.println(" FINAL OUT PUT >>> " + formatPrecision("173449.999"));
private static String formatPrecision(String s) {
if (s == null || s.trim().length() == 0) {
return "0.00";
try {
if (Double.parseDouble(s) == 0) {
return "0.00";
catch (java.lang.NumberFormatException nfe) {
return s;
double d = 0.00;
int ind = s.indexOf('.');
String dec = "";
if (ind > 0) {
dec = s.substring(ind);
if (dec.length() == 1) {
s = s.concat("00");
return s;
if (dec.length() == 2) {
s = s.concat("0");
return s;
if (dec.length() == 3) {
return s;
if (ind == -1) {
return s.concat(".00");
try {
d = Double.parseDouble(s);
BigDecimal bd = new BigDecimal(d);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); //2 decimal places
d = bd.doubleValue();
NumberFormat formatter = new DecimalFormat("0.00");
return String.valueOf(formatter.format(d));
catch (java.lang.NumberFormatException nfe) {
return s;
} -
I've got this sum for a 'shopping cart' style design and want the prices to round to 2 decimal places
I know I've got to put Math.round... in somewhere, but I just don't know
Script:
<script type="text/javascript"><!--
function updateultra() {
document.form.ultra.value = (document.form.sum1.value -0) * (1.79); }
//--></script><script>
function updateultra() {
document.form.ultra.value = Math.round((document.form.sum1.value -0) * (1.79));
</script> -
How do I correct % complete to show whole numbers, not two decimal places
I just installed version P6 6.2 on my laptop and restored a schedule. The percent complete is showing two decimal places for activities less than 100%. What I would like is to have whole percentages, e.i. 84%, not 83.76%. How do I correct this? I thought it would be under the user preferences, but it's not there.
Edited by: 795589 on Sep 20, 2010 5:46 AM
Edited by: 795589 on Sep 20, 2010 5:47 AM*795589,*
What particular percent complete are you having trouble with?
*(Found in the P6 knowledgebase) This issue is an existing Enhancement Request. It will be considered for a future release*
Other than that, maybe try creating a global change to manually calculate your percents.
hope this helps,
+*** Can the number of decimal places for Percent Complete be changed? [ID 895884.1] Modified 30-OCT-2009+ -
Round float to 2 decimal place in the table
Hi,
One field in a table in my DB has float datatype.
I insert some data into this table. The value read from a textbox I input as 0.02 for example. after insertion, I select the table and the field of the table shows the number as 0.0199999995529652
Where did I do a wrong thing? how can I make it exactly as 0.02 in the table?
Thanks.Hi there
Float data types have known problems with precision, see
here
Since FLOAT and REAL are approximate numbers, you can run into all kinds of presentation / storage issues if you use them inadvertently.
They have their place, and if you are using scientific data that requires the properties of floating point numbers, th they are the right choice. But in most cases, I think this is another evolutionary thing that happens accidentally when you upsize
from Access. You are much better off using DECIMAL or NUMERIC (which are functionally equivalent) unless you really know that you should be using FLOAT.
You should consider changing the datatype to decimal or numeric if you need it to be more precise. -
Convert rounded number into two decimal places
Hi All,
I have a Number 156 stired in a column having number(10,2);
but i want to update it into 156.00......
May be simple,,,,but how?
Edited by: 887268 on May 29, 2012 10:04 AMHi,
156
156.00
156.0000000000000 and
00000156.0000 are all the same NUMBER. There's nothing to convert.
If you want to display a NUMBER in a certain way (e.g., with 2 digits after the decimal point) then use TO_CHAR, or have your front end format the column. In SQL*Plus, for example, you can use the COLUMN command:
COLUMN sal FORMAT 99999.99
SELECT ename
, sal
FROM scott.emp
;Output:
ENAME SAL
SMITH 800.00
ALLEN 1600.00
WARD 1250.00
JONES 2975.00
... -
Issue rounding result to 2 decimal places
Good day all,
I currently have a format script to get the average of 10 fields on my form that also skips empty fields. Here is my current script:
// Initialize variables
var num = 0;
var sum = 0;
// Check input fields
for (var i = 1; i < 10; i++) {
var f = getField("Assesment_Rating" + i);
if (f.valueAsString) {
// increment the non-blank field counter
num++;
// add the field value to the running total
sum += +f.value;
// Calculate the average
if (num) {
event.value = sum / num;
} else {
// If fields are empty, so set to blank
event.value = "";
As it currently works if I have a total of 1.4444444444444, that is what is displayed. I've tried a couple of Math.Round functions that give me "1.5" as a result which is not correct. I'm still learning so I may have tried a Math.Round function in the wrong part of my code, but cannot get the result I need.
Thanks in advance for your assistance!That script should be a custom calculation script, which sets the field value. You can then set the format type to Number and two decimals.
-
Pages rounding up numbers in a .doc file to 2 decimal places
Hello, I have a problem, with Pages (3.4) on OS 10.7.5...
When I go to open a .Doc file, Pages seems to round up some numbers to 2 decimal places. I did find another thread which dealt with this problem inside of Tables, but I am not using tables in this document.... another problem is that the new rounded up number is not editable in Pages once the document is open. e.g.
In word/open office I see the figure (which is in fact a series of dates - i did not write this document btw)
12.12.24.1.13
in Pages is only see
.13
and this .13 is totaly un-editable and unselectable.
Can you help ?Got it!
I tried opening it in TextEdit but the funky dates were omitted from the page. Then I opened it in LibreOffice and it looked OK. The problematic dates seemed created by a special Word function. The behaving dates were manually written.
I selected all content of the document in LibreOffice and pasted into a Pages document and all dates came out as they should.
Maybe you are looking for
-
Hi, Been trying to work this out for hours now so if anyone can help I will be forever thankful! Basically, I traced an image earlier, expanded it then wanted to change the line colour. For some reason I couldn't get this to work even though I've don
-
How to find detail of when/who created particular entry in Z tabel
Hi folks I have a Z table, and which contain one entry with incorrect data . Is there any way to find out , that when and who inserted that resord in Z table Regards PG
-
I have a black band across my screen is it broken
I have got a black band across my screen on my I pad don't know how this has come is it an error or something, or is it broken.
-
I made an abortive attempt to upgrade from PSE 4 to PSE 7 in May. When I had difficulty converting my PSE 4 catalog and did not have time to deal with it, I de-installed PSE 7 and reverted to using PSE 4. Now, I am trying to update again and have 3 q
-
Linux(suse 10.2) PPC
hi, where can i found a jdk and an IDE that works on linux(ppc) and it's possible i m looking for a J2EE server thank u