Printing exactly 2 decimal places
I'm trying to print my double type variables to exactly 2 places. but numbers such as 3.00 and 124.00 keep printing as 3.0 and 124.0. Is there an easy way to force the printing to two decimal places on a primitive double type value without having to cast it to a Double object and playing around with NumberFormat and all that entails?
thanks much,
jh
darelln has exactly the right answer, except that you must do the thing you don't want to do. If it were me, I'd go with his example. It's less work and much cleaner.
import java.text.*;
public class DoubleTest {
static double[] numbers = {3000, 124.12, 123.122, 3.0, 124.0, 10.2, 12.14, 1234.55};
static final NumberFormat f = new DecimalFormat("#.00");
public static void main(String [] args) {
System.out.println("Goofy way!\n");
for (int i=0 ; i<numbers.length ; i++) {
printD(numbers);
System.out.println("\nRight way!\n");
for (int i=0 ; i<numbers.length ; i++) {
System.out.println(f.format(numbers[i]));
static void printD(double dbl) {
if (dbl % 10 == 0) {
System.out.println(dbl + "0");
} else {
String sdbl = "" + dbl;
String predot = sdbl.substring(0, sdbl.indexOf("."));
String atdot = sdbl.substring(sdbl.indexOf("."), sdbl.length());
String postdot = sdbl.substring(sdbl.indexOf(".") + 1, sdbl.length());
if (postdot.length() == 2) {
System.out.println(sdbl);
} else if (postdot.length() < 2) {
System.out.println(predot + "." + postdot.substring(0,1) + "0");
} else {
System.out.println(predot + "." + postdot.substring(0,2));
java DoubleTest
Goofy way!
3000.00
124.12
123.12
3.00
124.00
10.20
12.14
1234.55
Right way!
3000.00
124.12
123.12
3.00
124.00
10.20
12.14
1234.55
and the results are identical!
Similar Messages
-
Printing to two decimal places on the screen
Hi everyone...
I am using mysql as my DB...my field is declared there as decimal(8,2) means to 2 decimal place..when i am getting the result i also need to print to 2 decimal place...
so when i do(rs.getDouble("Price")) it is not printing as two decimal place...how do i get it to print to 2 decimal place...
Thanx a lottttttjavax.text.DecimalFormat dfrm t=new java.text.DecimalFormat("#0.00");
//or another format see API doc for DecimalFOrmat
String result = dfrm.format(rs.getDouble("Price")); -
i have my database in mysql and my field total is declared as decimal to 2 decimal place.....how to i get it to print to two decimal place in my JSP page...i have declared the variable as double but when it is printing it is printing to one decimal place only...please help
don't declare ur column as decimal in MySQL . Declare as decimal(3,2);
thanx,
Samir -
Printing only 2 decimal places
Hi people, thank again in advance for your help.
if i have a number with tons of decimal places behind it...and only wantto print on screen 2 (for instance when I am want $10.00 instead of $10.000000001) or somethign like that, what can i do?
With Math.round() work? but i also want such that if the figure is zero....to be printed $0.00 and not $0.0.
Can anybody helo?Thanx mark. but when i tried i get these errors.
C:\WINDOWS\Desktop\BC201\assignment2\Savings.java:86: cannot resolve symbol
symbol : class DecimalFormat
location: class Savings
DecimalFormat df = new DecimalFormat(".00");
^
C:\WINDOWS\Desktop\BC201\assignment2\Savings.java:86: cannot resolve symbol
symbol : class DecimalFormat
location: class Savings
DecimalFormat df = new DecimalFormat(".00");
^
2 errors
Tool completed with exit code 1 -
Print Doubles to two decimal places?
When I do calculations on my double numbers and then print them on screen I get a string of digits after the decimal point. Can anyone give me the code as to how I use the DecimalFormat class to make these digits print to 2 decimal places. I am sure it is very simple line or two of code but I am unsure how.
Thanksdouble number = 123456.789;
DecimalFormat df = new DecimalFormat("#,##0.00");
System.out.println(df.format(number));
DesQuite -
Print Scale Percentage - Can the percentage go more than 2 decimal places??
Hello All,
Illustrator CS6
I want to know if it is possible to get more than 2 decimal places in the Print Scale Percentage option box (as shown below)?
It seems to round to 2 and I would like to get at least 3 for better accuracy.
Any thoughts?
Thanks
GeoffNot that I know of. If you need it to be more exact, resize the artboard and scale the content. It's an arguable point, anyway, given how your printer will translate/ rasterize the stuff. Even if there were more digits, the result after the halfftoning might not at all be any different...
Mylenium -
PO print preview displays incorrect setting in decimal places
Dear Experts
Please help me on the following condition.
I have a problem in print preview of a PO. It displays the incorrect setting for decimal place in currency.
It shows USD 1.404,00 whereby the amount should be USD 1,404.00
This only happens to a certain vendor that is doing PO for the first time from that country
Example:
I'm creating a PO for vendor 123. Vendor 123 is located in country A. No PO has been created for any
vendor in country A before. When I saved my PO, in print preview, the decimal setting is not correct.
But when I'm creating another PO, for vendor 234. Vendor 234 is located in country B. There have been
several POs created for vendors in country B before. When I saved my PO, in print preview, the decimal
setting is correct.
Both vendor 123 and vendor 234 is using currency of USD.
Is there any other setting with the vendor master in regard with the currency decimal setting for each
country for the vendor? Or something that I've missed out?
Diagnosis:
I've checked with the user profiles->default-> decimal notation. It displays 1,234,567.89 correctly
I've also run OY04, but the decimal setting is correct.
Thank you
Regards
Syukrithis has nothing to do with print preview, it is just basic country setting (OY01) how a quantity and value is written on a PO to a vendor is country xyz.
German uses to comma to seperate decimals, while USA uses the point to seperate decimals
this would be the German version 1.404,00, and this the US version:: 1,404.00
so if an American sends a PO to a German vendor, then the document will show 1.404,00 so that the German can understand that he wants 1404. (and not 1000 times less) -
How to get the exact decimal places displayed while exporting to Excel?
Hi All,
I have a requirement to display the exact decimal place values in my report. In my pivot table, i duplicated a column named order number and converted to percentage by applying these conditions Show Data As-> Percent Of-> Column. I got a resultant column with one decimal point. As i needed two decimal points i took the xml of the report and changed the minDigits='2' and maxDigits='2' values like this. After that i got the accurate answer. But when i export it as excel sheet it is not showing the % column properly. The values after 0 is not getting displayed. Any solution for this?
Thanks in Advance
ThenmozhiHi Thenmozi,
You can try adding the below statement in the Custom CSS section of the your column properties.
mso-number-format:"\@"
Edit your report--> Criteria-->which column you want to display in excel click column properties --> select CCS--> Paste here above code.
Second way:
Edit your report --> criteria-->click fx option for your column and apply the below code.
2. '<span style="display:none"> </span>' || CAST(your column name AS CHAR)
Award points it is useful.
Thank,
Satya -
Printing a double to accuracy of 2 decimal places
Hi,
I have a double value and I want to print it to an accuracy of 2 decimal places. Is there any way to do this. I tried using the DecimalFormat by
doing the following
String str = Double.toString(value);
DecimalFormat dec = new DecimalFormat(str);
dec.setMaximumFractionDigits(2);
String res = dec.toString();But this doesn't seem to work. I am using java 1.4.
ThanksYou can use DecimalFormat but like this:
double value = 1.345678d;
System.out.println(value);
DecimalFormat dec = new DecimalFormat("0.00");
String result = dec.format(value);
System.out.println(result);Or you can use a BigDecimal where you can specify the most appropriate rounding method:
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(bd.toString());HTH,
Christian -
Help!printing float values with two decimal places
hi there java pips! im a newbie to this technology so forgive me for this really stupid question....i would like to perform mathematical operations on two float values....the problem is i want to print them in a standard format and that is i want them to be displayed with two decimal places (e.g 190.00, 12,72, 1,000.01) how can i do this?
Try java.text.DecimalFormat
NumberFormat nf = new DecimalFormat("0.00");
System.out.println(nf.format(x)); -
ALV output is 2 decimal places, but prints as 3
Hi,
In my current ABAP program, the output is shown as 2 decimals, using the following domain:
Data Type: QUAN
No. Characters: 13
Decimal Places: 2
Output length: 17
However, during unit testing, it was found that during printing, instead of printing it out as 2 decimals (ex. 12.00,) its printed out as 3 decimals (ex. 12.000.)
The same thing can be noticed when using Print preview. It starts at the editable fields and continues onward to others.
Why is this occurring and what can I do to fix this problem?
Points will be rewarded and all help will be greatly appreciated.
Thanks,
JohnHi
Check once again the attributes of that Qty field in Dataelement and domain
Because for all QUAN type fields it is always 3 DECIMAL places
you are saying it is 2
check it
All the qty fields in SAP with data type QUAN uses/prints 3 decimals
so check and see
like fields LFIMG.MENGE,FKIMG,KWMENG all fields with 3 decimals
Reward points for useful Answers
Regards
Anji -
Printing two decimal places from BigDecimal values
I am using BigDecimal to represent money values. My output needs to line up so that (with a non-proportional font) the decimal point and the two decimal places are in the same columns for each line. But when the dollar value has zero cents, or has a number of cents that is divisible by ten, it drops the trailing zeroes, and drops the decimal point if the decimal places are both zeroes. For example, I want to see "25.00" instead of "25" and "25.50" instead of "25.5". It doesn't seem to make any difference if I set the scale to 2. (I have resorted to getting the toString() of the BigDecimal and hacking the string before I display it, but surely there must be an easier way?)
BigDecimal aaa = new BigDecimal("25");
BigDecimal bbb = new BigDecimal("25.0");
BigDecimal ccc = new BigDecimal("25.00");
BigDecimal ddd = new BigDecimal("25.5");
BigDecimal eee = new BigDecimal("25.50");
BigDecimal fff = new BigDecimal("25.75");
aaa.setScale(2);
bbb.setScale(2);
ccc.setScale(2);
ddd.setScale(2);
eee.setScale(2);
fff.setScale(2);
System.out.println("SCALE SET TO 2: ");
System.out.println("aaa = " + aaa);
System.out.println("bbb = " + bbb);
System.out.println("ccc = " + ccc);
System.out.println("ddd = " + ddd);
System.out.println("eee = " + eee);
System.out.println("fff = " + fff);produces this output:
SCALE SET TO 2:
aaa = 25
bbb = 25.0
ccc = 25.00
ddd = 25.5
eee = 25.50
fff = 25.75Thanks,
MartinThankyou Dr. Clap. This solved my problem - I added an LHS to the setScale statements:
aaa = aaa.setScale(2);
bbb = bbb.setScale(2);
ccc = ccc.setScale(2);
ddd = ddd.setScale(2);
eee = eee.setScale(2);
System.out.println("SCALE SET TO 2: ");
System.out.println("aaa = " + aaa);
System.out.println("bbb = " + bbb);
System.out.println("ccc = " + ccc);
System.out.println("ddd = " + ddd);
System.out.println("eee = " + eee);produced:
SCALE SET TO 2:
aaa = 25.00
bbb = 25.00
ccc = 25.00
ddd = 25.50
eee = 25.50 -
New question....adding decimal places...
Ok my assignment is thus:The Lone Oak High School Marching Band is raising funds by selling fruit. In order to simplify processing of orders, only three types of packages of fruit are being sold. They are
Box of Apples at a cost of $14.00
Box of Peaches at a cost of $16.00
Box of Oranges at a cost of $18.00
There is a 10.00% tax on the total order amount. Write a Java program that reads the number of each type package on an order and computes the cost for the packages of each type ordered. Total the order, calculate the tax on the order, and the total including the tax. Print all items read and all items calculated with appropriate labels.
Example:
Boxes of Apples: 2
Boxes of Peaches: 3
Boxes of Oranges: 4
Total cost of the fruit: 148.00
Tax: 14.80
Total Amount Due: 162.80
Now ive got the program to work exactly as it should but for neatness i want it to have 2 decimal places. When it runs, ill use the above as an example, it cuts off the second decimal. Is there a nice way to add the the second decimal place?
Edited by: chaoticglee on Jan 30, 2008 3:40 PMTry this as an example:
import java.util.Scanner;
import java.text.DecimalFormat;
public class ex
final static double HOTDOG_COST = 10.00;
final static double TAX = 0.05;
public static void main(String[] args)
DecimalFormat money = new DecimalFormat("$0.00");
Scanner in = new Scanner(System.in);
System.out.print("How many hotdogs do you want? ");
double subTotal = in.nextInt() * HOTDOG_COST;
double taxCost = subTotal * TAX;
double total = subTotal + taxCost;
System.out.println("Cost of food: " + money.format(subTotal));
System.out.println("Tax: " + money.format(taxCost));
System.out.println("Total: " + money.format(total));
} -
DIsplaying WRBTR (AMOUNT FIELD)WITHOUT DECIMAL PLACES
Dear all,
I am facing a problem in displaying amount field in alv report. I have two currency keys (US dollar and Japan yen). if it is japan yen I should not have to show decimal places and if it is US dollar I should show decimal places.
How can I do it in a alv report if I have only one output amount field?
thanks
Ranjan.Hi chittaramjan,
1. use this kind of funda (just copy paste in new program)
Important things
a) one extra field in internal table
declared in the same exact fashion
waers LIKE tcurc-waers,
b) Populate this field
c) REST THE System will take care
(based upon country settings)
2.
REPORT abc.
TYPE-POOLS : slis.
DATA : alvfc TYPE slis_t_fieldcat_alv.
DATA : BEGIN OF itab OCCURS 0,
f1 LIKE P0008-BET01,
waers LIKE tcurc-waers,
END OF itab.
START-OF-SELECTION.
itab-f1 = '-4.15'.
itab-waers = 'KRW'.
APPEND itab.
itab-f1 = '4.68'.
itab-waers = 'GBP'.
APPEND itab.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid
i_internal_tabname = 'ITAB'
i_inclname = sy-repid
CHANGING
ct_fieldcat = alvfc.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = alvfc
TABLES
t_outtab = itab
EXCEPTIONS
OTHERS = 2.
regards,
amit m -
Decimal places in Query generator
Hi All,
I am observing a weird behavior in Query generator's execution of a simple sql query. the query is :
Select 0.002834
now, in the general settings - display - amounts , I put the decimal value as 4 or 6, then only it prints 0.0028 or 0.002834.
else it prints 0.00 (if the decimal place is 2). it should ideally have nothing to do with the display of amount. any idea?
(the effect is shown only after you change the decimal display, update it and close and reopen the SAP application.
Thanks,
Binita
Edited by: Binita Joshi on Apr 12, 2010 4:03 PMHello Binita,
>I have tried converting it to numeric(19,4) and numeric(19,6) but still the result was same.
Check your decimal places settings in
\Administration\System Initialization\General Settings\Display tab
Check the following values:
- Units (For meaurement)
- Decimals in Query
Now let' s say,
- Unit set to 6 decimal places
- Decimals in Query set to 2
run the following query in query generator:
select cast(0.000245 as decimal(19,6))
result will be
0.00
run the same query as FMS on item master data in any measurement field (lenght)
select cast(0.000245 as decimal(19,6))
result will be
0.000245
Now set the - Decimals in Query set to 6 on \Administration\System Initialization\General Settings\Display tab
run the following query in query generator:
select cast(0.000245 as decimal(19,6))
result will be
0.000245
This is a normal habit of SAP B1 rounding engine / displaying engine.
Regards
J.
Maybe you are looking for
-
Download stops/stuck at 99%...only .exe files
whenever (from last 2 years) i try to download an binay file .exe file...the download never compeletes....it stops at 99 %...in this way i cannot update my adobe flash player or mozilla firefox....i have no antivirus... firewall turned offf.....i tri
-
What is the table field for PO releaser in R/3 (4.7 Enterprise)?
Hi Gurus out there: Do you know if there is a table field for PO releaser in R/3 (4.7 Enterprise)? We use for e.g. the transaction ME29N to release a PO and I haven't been able to find a DB table where the PO releaser is stored. In DB table EKKO I ca
-
Document Server not working properly on Vista and website
I just got a new PC with Vista Home premium edition and Document server is definitely not working the same as it did on my XP. I know it's an old program so I guess it's finally be time to buy the new stuff but why won't it work on the website I util
-
I have an mdi applicaton in Visual studio 2010 (.Net framework 4.0). I'm having a flickering problem when user switch between an MDI Child form. I'm declaring the child form then setting the borderstyle.none at design time and dock property to fill t
-
I am trying to restore my contacts from icloud to iphone 4 but when trying to switch off contacts in icloud first to switch back on it just hangs for ever - dont know what else to do - help please