Rounding off the decimal place.
how to round off to 2 decimal place for this following programs:
double x;
x = 5.0/3.0;
System.out.println(x);
answer for x is 1.6666666666666
my desired output is 1.67
import java.text.*;
public class Foo {
public static void main(String[] args) {
double x;
x = 5.0 / 3.0;
DecimalFormat df = new DecimalFormat("0.##");
System.out.println(df.format(x));
}
Similar Messages
-
How to restrict the decimal place of a floating point number?
Hi,
Here is my code:
public void TwoDecimal(double u){
String w = Double.toString(u);
int c = w.length();
System.out.println(c);
if (c <= 5)
double a = Double.parseDouble(w);
System.out.println(a);
else
System.out.println("Invalid input!");
}I want to show a floating point number which has 2 digits and 2 decimal places, e.g. 45.82, 29.67. This number is input by user and passed as a parameter.
For those case like the above sample floating point numbers, it can display the proper value of 'c'. e.g. 45.67 will display 5.
However, when I passed 99999, it will show 7; 9999 will return 6, not 5.
So, if the user does not input the '.', does it append 2 implicit chars to it? i.e. 99999.0 and 9999.0. So, that's why it returned 7 and 6 for the length of the string respectively.
How can I fix it?
and
Does it has better algorithm?
Pls advise.
gogoWhen dealing with a known precision, in your case hundredths, it is often a good idea to use an integer type and add in the decimals on printing only. This is often the case in banking systems. Almost all of them use integer types, (read long) in pennies to store monitary values. Ever seen someone type in a value for a credit card machine? For something like $20 they press.. "2" "0" "0" "0" The machine knows the lowest denomonation in a cent, so it knows where to put the decimal place. I suggest you do something like this. It also helps to avoid base 2 round off errors.
-Spinoza -
Rounding To Nearest Decimal Place
Hey Flash Family,
I am getting the correct output i want but is also giving me
like 8 numbers after the decimal place...all i want is the numbers
rounded to the nearest cent meaning only 2 numbers to the right of
the decimal point....what do i need to add to the current code to
correct this issue.
Thanks N Advanceattach that code to the first frame of your main timeline.
then anytime you need to change the format of a number like
_root.how.ROIM.val_tcost use:
_root.return2DecPlaces(_root.how.ROIM.val_tcost); -
Rounding to 2 decimal places in Derived column transform editor
I want to replace the contents of a value column with itself but rounded to 2 decimal places.
The current column is a double and I have tried to perform this using the following expression but it fails to work.
Code Snippet
Round(cc_vl,2)
How should I achieve this?I’ll take a guess here but you started with this –
123.45678
Got this –
123.45000
Wanted this –
123.45
So the 123.45000 is absolutely correct, the value has been rounded to 2 digits. A double however has a scale greater than 2, and you only wanted two digits after the point because you were concerned about how the value looked rather than the actual value. This is certainly not a bug. It is how you format the data for display.
Your solution is the way to do this, either on the Derived Column or Data Conversion Transformation. You got it in the end, the replace versus new, because you cannot change the type of a column. You may convert the value, but the storage type of the column means it will implicitly revert to the base column type when you put it back, albeit with a value that has been rounded.
Does that explain it? -
Hi,
I am trying to round the decimal place of number 0.105 as .11.
But it only rond up to 0.1 not 0.11.
I don't understand why it doesn't round to 0.11.
Can any one explain to me ?
My code :
import java.util.*;
import java.text.*;
import java.math.*;
public class TestFile {
public double number = .105;
public static void main(String[] args) {
TestFile test = new TestFile();
DecimalFormat aFormat = new DecimalFormat("#.##");
int decimalPlace = 2;
double num = .105;
BigDecimal bd = new BigDecimal(num);
bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
//DecimalFormat aFormat=new DecimalFormat("###.00");
String formattedString = aFormat.format(test.number);//format the String entered
System.out.println("formatted string: " + formattedString);
System.out.println("big decimal " +bd.doubleValue());
double formattedDouble = Double.parseDouble(formattedString);//convert to a double
System.out.println("back to a double: " + formattedDouble);
What should I do to round 0.11 ?
Thanks,
njan.BigDecimal bd = new BigDecimal(num);Change this line to
BigDecimal bd = new BigDecimal(Double.toString(num));Read the notes in the API for BigDecimal constructor that accepts double.
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
The results of this constructor can be somewhat unpredictable... -
Regarding round off the value in adf table
HI All,
My Requirement:
i have to round off the value after decimal in adf column before saving to data base.
Excample : 1234.67 = 1235
34567.89=34568
34567.34= 34567
i am using jdeveloper 11.1.1.3.0
Please can you give code using converter or any other strategy.
Thanks & Regards,
Madhu1. First of all you asked for rounding off, taking the integer part is not rounding off.
2. As per the documentation
integerOnly boolean Yes Flag specifying whether only the integer part of the value will be formatted and parsed. Default value is false.if i can understand correctly pertains to formatting only.
3. You can create a custom faces converter and add it to faces config.xml http://docs.oracle.com/javaee/1.4/tutorial/doc/JSFDevelop4.html . So that you do not have to write it in code.
4. If only integer is to be allowed why don't you change the data type to long or int instead of number -
The decimal place is wrong in GR55
Dear Expert:
We are using currency JPY
We now creating a new report using report painter, but unfortunately the decimal place is wrong,
the result is only one percentage of the real amount, how can I get this issue solved?
notice that we are using report painter, not ABAP programming, so function module BAPI_CURRENCY_CONV_TO_INTERNAL
is not applicable.Dear Lu Meng
I was reading your question which seems not answered at all.
I have EXACTLY your same issue.
In ECC using a FAGLFLEXT Report Painter.
My zero decimal currencies such as HUF and JPY are showing in the report, with decimals, thus are factor of 100 too small. Even changing layout deimcals, etc has NO EFFECT.
Did you solve this sissue? OSS has nothing either?
Thanks for any assistance.
Richard -
Changing the Decimal places in the currency
Hi all
we are using the currecy SAR. Decimal places was set to 4, now that they want to revert back to 2 decimal places. i understand that by doing so they would be inconsistencies in the data allready recorded. how to avoid that?
is there any programmes / reports that would fix such inconsistencies?
any OSS notes?
thanks and regards
gkkHI,
You should NOT change the decimal places in a productive system.
If you would do, the correction of the posted documents afterwards would be a remote consulting issue. Which you have to pay for SAP.
You see, all posting you have done until now, would get displayed in a wrong way, and the new posting wouldn't compare to the others.
Please check the note 137626 describes this.
If you really need to change the decimals, you should create a new currency type, for example your current is SAR, and now you should create a SA2. Then you should enter new rations and exchange rates for it, and work with the new one.
Also if you want to set the decimals to two digits from four, youshould not enter an entry in the OY04(TCURX).
In this table you can only enter a value, that discriminate from 2 decimals. If there is no entry in the table, it means the currency has two decimals.
Please assign points if it useful.
Regards
Ravinagh Boni -
Inscrease the decimal Places in Tcode QA32(UD)
Hi Guys,
I want to increases decimal point in Tcode QA32 .
My user requirement is to increases the decimal places from 3 to 4 in Inspection lot stock / to be posted.
We maintain 3 decimal places & I want 5 decimal places at the time of Usage Decision. But I couldn't found the required customizing setting.
Please suggest me....Hello,
Changing Data Types and Lengths of Existing Fields
If a data element is assigned to the field, you must change the technical field attributes (data type, length, number of decimal places) in the domain of the data element.
Procedure for Fields with Data Elements
Double-click on the name of the data element.
The data element maintenance screen appears.
Double-click on the name of the domain.
The domain maintenance screen appears.
Choose Domain ® Display ® Change.
Make the required changes and save your entries.
Note that this change affects all the table fields that refer to the particular domain. Foreign keys could become inconsistent and conversions required. You should therefore consider the effects of this action before changing the domain by choosing in the maintenance screen of the domain and checking if the domain is used in other tables.
Choose .
The table is activated again and the table field is adjusted to the domain change.
Procedure for Fields with Direct Type Entry
Place the cursor on the field and choose Data element / Direct type.
Values can now be entered in fields Data type, Length, DecPlaces and Short text.
Change the entries for the data type, length and possibly the number of decimal places.
Save your entries.
Choose .
Note that a table conversion will normally be necessary (see Adjusting Database Structures) if you change the field attributes
NOTE: You need to have Access key.
Regards,
Alok Tiwari -
Without to change the setting.how to set the decimal place to 7 for an UDF.
Without to change the setting, how to set the decimal place to 7 for an UDF?
Hi
Once you create a UDF it will automatically be binded to the data source of data type that you gave at the time of creating UDF.
So i think you can not set the decimal places without changing the setting in display parameters.
i am not sure whether it will work or not one thing you can try is that create it as a alphanumeric data type and convert your decimal value to sting and then assign it to UDF,While retrieving for any manipulation you convert it as decimal.
Hope it helps you
Regards
Vishnu -
Change the decimal places of a field in screen table control
HI ,
I have a MENGE field having data element as , BSTMG (decimals 3 ).
This field MENGE , I have used in a table control. The value in this field is filled from a custom Z-table where the same data element BSTMG is used. So , for eq.) value in MENGE field is shown as 6.010
But the requirement is that the value should be displayed as only 2 decemals i.e.) 6.01
I dont want to change the domain of the field in the Z-table , to 2 decimal places , because the table is used in other developments also , so it will give a dump there which I have to correct in other developments also.
So , is there any way to change the decimal places of a field in screen table control , without changing the domain , in the table from where the value is fetched... ????
Edited by: abhijeet_7013 on Feb 14, 2011 3:18 PMHi, create a structure in local program with same field names and change the TYPE with some 2 decimal domain of MENGE. Populate this local stucture and use it in TABLE CONTROL.
Hope this helps...
Cheers -
Removing the decimal places of currency field
Hi Gurus,
I have a currency type field ( to be more specific a field like BSEG-WRBTR) .
How do i eliminate the decimal places for this field.
ie if the value is 345678.91, i need to make it 345678.
Is there a standard keyword or something for this?
Points will be given to all the helpful answers.
Thanks,
Nobin.You can use the FLOOR function to get the largest integer value without decimal.
Like:
DATA: W_RESULT TYPE I.
W_RESULT = FLOOR( P_AMT ).
Regards,
Naimesh Patel -
When using Math.round, I know to round to 2 decimal places it would be Math.round(variable * 100.0)/100.0. I have this in one program and it works. (answer = Math.round(answer*100.0)/100.0;) I have it in another program and it doesn't (double answer = Math.round(balances [looper]*100.0)/100.0;// balances is declared as double balances [] = new double [numberOfCustomers];) What am I doing differently that I should be looking for.
Hi,
Please try using java.text.DecimalFormat class. This class is a subclass of java.text.NumberFormat. You can read about this class from JDK documetation.
HTH
VJ -
Regarding Ronding off the decimal value
Hi,
i have a decimal number as " 58240990.00 " , i wanted this to rounded value .
for example I am expecting to see 58241 for the above number.
shell we can do in any way.. if so please let me knw fast.. it is urgent..
thanks in advance.
thanks,
Suresh..Hi Suresh,
Use <b>ROUND()</b> numerical function.
ROUND(a) round decimal places up and down
ROUND(a,n) round up and down to the nth place on the right of the decimal point
ROUND(a,-n) round up and down to the nth place on the left of the decimal point
DATA V_F TYPE F VALUE '58240990.00'.
DATA V_ROUND TYPE I.
V_ROUDN = ROUND(V_F,'-4')
Thanks,
Vinay -
Rounding to two decimal places in BPC/NW
Hello,
I am dealing with the task to round values to two decimal places. I have found some discussions in BPC-MS where the statement ROUND should work in logic. Unfortunately I am not able to make it work in the NW version.
Isn't there also a parameter in the appset or application administration which would cut the numbers only to two decimal places (this task would be also a possible solution for us).
Thanks for any help
JanHello Pravin,
the second import worked fine. I realized that the class has changed to ZCL_BPC_SL_ROUND.
I debugged the class and noticed that the parameter it_cv is not filled at all. Therefore the program ends before starting the rounding procedure. Here is the part of the code:
Take CV into account
clear l_success.
loop at it_cv into ls_cv.
clear l_val_string.
loop at ls_cv-member into l_member.
if sy-tabix eq 1.
l_val_string = l_member.
else.
concatenate l_val_string l_member into l_val_string separated by ','.
endif.
endloop.
if l_val_string ne space.
l_success = add_dim_restriction( i_param = l_val_string i_dimension = ls_cv-dimension i_clear = abap_true ).
if l_success eq abap_false.
concatenate 'Failed to successfully add Dimension' ls_cv-dimension into l_log_msg
separated by space.
cl_ujk_logger=>log( l_log_msg ).
cl_ujk_logger=>log( 'Exiting Round Method' ).
cl_ujk_logger=>empty_line( ).
raise exception type cx_uj_custom_logic
exporting messages = et_message.
exit.
endif.
endif.
endloop.
if l_success eq abap_false. exit. endif. - here the program exits and therefore doesn't do any rounding.
If I comment this part the system rounds correctly but runs on all records in the cube which degrades performance a lot.
Could you please advice further if it is possible to restrict the data region only to the submitted data?
Best regards
Jan
Maybe you are looking for
-
How do I stop getting email notifications every time someone replies to a post I commented on?
I have changed my settings to "no" for ALL email options in the preferences pane, and am still receiving tons of emails. I commented on an apparently very popular thread a few days ago, and every few hours I get 5-10 new emails that are all just new
-
Why do I have to buy Transformers II ?
... and other movies I've never liked that online video rentals are 24 hours when the old brick-n-mortar gave me a week or two. I would rent more if I was given even a few days - like through the weekend, 'ya know? The price of renting online bugs to
-
Hotfix available now for Creator 2 and Creator 2 Update 1
Have you accessed the AutoUpdate Center lately? Check out the latest hotfixes for both Java Studio Creator 2 and Update 1. Open the IDE, connect to the Internet and visit the Update Center to download the available modules. For Update Center instruct
-
Struts PgFlow error using a RowSet control in WL 8.1 SP1
I created a RowSet Control within an existing WAR and setup a PgFlow process to CRUD my linked datasource. When i run or view the pageflow it builds and deploys fine and I am able to view the generated main grid.jsp containing all my data i
-
Downloading link for adobe creative suite 5.5 design standard middle east please
I have my serial for the ME CS 5.5 DS but I can't find the download link for this vision .. the installation in my CD didn't work it.. I tried to download from the help page Download CS5.5 products but I couldn't find the Middle East version .. Can a