Decimalformat
hi all
Decimalformat depends on underlying platform for
rendering certain symbols.can anyone help me in this problem
Hi charanabc,
In the decimal format class, when you specify the
pattern for formatting, there is a special symbol
which takes care of Currency. it is '?'. At runtime
this will be replaced by '$'. While this works fine
in windows, when you run the programme on unix, sometimes it
just doesnt render the $ sign. Even though the Decimal
Format was initialized for a particular Locale, it
still doenst work. The solution is that you will be
forced to set a unix env variable called LANG. Set it to
en_US or what ever and it starts behaving properly .
I hope you need this info.
Regards,
TirumalaRao
Developer Technical support,
Sun MicroSystems, India.
Similar Messages
-
JFormattedTextField - DecimalFormat
Hi.
I have a problem with JFormattedTextField.
I'm using this with an DecimalFormat with pattern ######0.00#####
JFormattedTextField = new JFormattedTextField(new DecimalFormat("<pattern>"));
Now when I enter a minus sign '-' as first charachter this is ignored and the text in the field is 0.00
I have tried using a negitive subpattern so the pattern is like
######0.00#####;-#####0.0######
But it doesn't work....
Help Please!!Try using a MaskFormatter.
-
Hi
I have developed a very small example but I have a problem and i don�t understand where is the error.
I have the String 10.000E+06 and the result is 10000.0.
Why the result isn�t 10000000?
It�s possible that is an error code?
Please help me.
Thanks
vraikibabthis is the simple code:
import java.io.*;
import java.text.*;
public class jf2 {
public static double val;
public static void main (String args[]) {
String str = "10.000E+06";
System.out.println("Original string: str="+str);
DecimalFormat pr = new DecimalFormat();
val = pr.parse(str, (new ParsePosition(0))).doubleValue();
System.out.println("DecimalFormat.parse() gives: val="+val);
Double Val = new Double(str);
System.out.println("Double.doubleValue(): val=" + Val.doubleValue());
}thanks -
Create a new subclass OverpunchFormat extends DecimalFormat
Well the last question on Formats I had got such quick and good results I'll give this form another go...
I've got a funny fixed field format I've got to produce call NSF Overpunch - used in the health care industry (NSF = National Standard Format). They decide to save a few of those expensive bytes and encode the sign of the dollar amount into the last (right most) digit of the number... also they imply 2 decimal points.
So if the number 234.56 is output in Overpunch format
it becomes 2345F;
23.00 -> 230{
-3.77 -> 37P
Via the encoding chart below.
So I can get the format for impling the 2 decimal digits - but now I need this sign convention applied.
The $640000{ question, can any NumberFormat do this?
Thanks, David
<code>
+ -
========
{ 0 }
A 1 J
B 2 K
C 3 L
D 4 M
E 5 N
F 6 O
G 7 P
H 8 Q
I 9 R
</code>>
But the doc for this method should normally tellyou
what is the final situation for that parameter, oram
I wrong?Well - yes it should tell me the side-effects to the
parameter.
But can you figure out what this means:
fieldPosition - On input: an alignment field, if
desired. On output: the offsets of the alignment
field.
That's the complete documentation on how DecimalFormat
effects the object FieldPosition - and I don't
understand it.In this case, I would read up on FielPosition class and see what it is used for. See if you need to use it or not. Basically, does your format have fields? If so, what are they? If not, don't over-ride the method, or do, but call your other format method and return its value, leaving the FieldPosition untouched. -
Java.text.DecimalFormat Error
I have this import directive in my jsp page.
<%@ page import="java.io.*,java.sql.*;java.text.DecimalFormat" contentType="text/html;charset=windows-1252"%>
I am using Jdeveloper 9i 9.0.3.
When I compile this page from within JDeveloper I get this error for java.text.DecimalFormat
Error: 'class' or 'interface' expected
When I compile outside JDeveloper, I get NO error.
Can somebody tell me what the problem is.<%@ page import="java.io.*;java.sql.*;java.text.DecimalFormat" contentType="text/html;charset=windows-1252"%>
I think that missing semi-colon is what's creating problems for you.
Sergio Bastos -
DecimalFormat scientific notation if necessary
I would like to find a way to output a double value in a way that uses scientific notation only if the number of digits output would be at least 15. For instance, the value 12345000000000000 should be output as 1.2345E16, but 12345000 would not be output in scientific notation (and the same logic if the value is < 0). Is this possible with the DecimalFormat class?
I tried using String.format("%.15g", value), but that always displays trailing 0s to fill the significant digits (e.g. 12.345 outputs "12.3450000000000"). I don't want it to output the trailing 0s.I would like to find a way to output a double value
in a way that uses scientific notation only if the
number of digits output would be at least 15. For
instance, the value 12345000000000000 should be
output as 1.2345E16, but 12345000 would not be output
in scientific notation (and the same logic if the
value is < 0). Is this possible with the
DecimalFormat class?
I tried using String.format("%.15g", value), but that
always displays trailing 0s to fill the significant
digits (e.g. 12.345 outputs "12.3450000000000"). I
don't want it to output the trailing 0s.With DecimalFormat ( http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html )
you can format the number in scientific notation but to control which numbers are formatted I think you must write some sort of if else -
DecimalFormat issues/Scientific notation
I have 2 issues with formatting numbers with scientific notation via the DecimalFormat class
ISSUE 1: Disregard of the number of MAXIMUM FRACTOINAL DIGITS
in the code:
NumberFormat nf = NumberFormat.getInstance();
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern("#00.#E0");
System.out.println( df.format(12345678));
it printed: 12.35E6
Why does it violate my request for one significant digit beyond the decimal
point?. (Note, this problem
only seems to occur when when the sum of MAX integer and Max fractional
digits in my pattern is 4)
ISSUE 2:
Number of significant digits displayed:- I really just need a sanity check on this one
The 1.4.2 API for DecimalFormat states
? The number of significant digits in the mantissa is the sum of the minimum integer and
maximum fraction digits, and is unaffected by the maximum integer digits. For example,
12345 formatted with "##0.##E0" is "12.3E3". To show all digits, set the significant digits
count to zero. The number of significant digits does not affect parsing.
I tried this ? it displays 123.45E3, or 5 significant digits? Looks like the number of significant digits
is MAX integer + MAX fractional digits in a pattern. Am I correct (and the API not correct)?
thanks
carolThanks. I'm assuming you're responding to issue #1. I did try it, and it worked, as expected. I never seem to have an issue when all symbols
preceding the decimal are 0. My issue, I suppose, is the inconsistency of how the formatting
works, when it comes to the number of fractional digit positions. Most of the times it 'behaves' and
only prints out the number of digits you ask, but sometimes it does not.
I've tested quite a few combinations. I'm attaching the code (in case you need help sleeping tonight).
The only 'pattern' I've noticed is that this issue only occurs when the total number of digits specified
in the pattern (before and after decimal) is 4. (exception ... if all digits specified before the decimal
are 0s, this never occurs)
I know a simple solution ... make sure I never have
a total of 4 #s and 0s in my pattern. But again, my question is why ... and/or ... does this type of
inconsistency crop up elsewhere.
import java.util.*;
import java.text.*;
public class x {
public static void main(String [] args) {
NumberFormat nf = NumberFormat.getInstance();
DecimalFormat df = (DecimalFormat)nf;
// these 3 work like I'd expect: 3 digits to the left, one to the right w/ rounding
// signif digits = max int digits + max fractional digits
df.applyPattern("000.#E0");
System.out.println( df.format(12345678)); // 123.5E5
df.applyPattern("00.#E0");
System.out.println( df.format(12345678)); // 12.3E6
df.applyPattern("0.#E0");
System.out.println( df.format(12345678)); // 1.2E7
// signif digits = TOTAL int digits + max fractional digits
System.out.println("X");
df.applyPattern("###.#E0"); // how did it decide to place decimal where it did?
System.out.println( df.format(12345678)); // 12.35E6 // why did it violate my "1 max fractional digit"
// request? I would have expected 123.5E5
df.applyPattern("##.#E0");
System.out.println( df.format(12345678)); // 12.3E6
df.applyPattern("#.#E0");
System.out.println( df.format(12345678)); // 1.2E7
//signif digits - TOTAL int digits + max fractional didgits
System.out.println("");
System.out.println("XXXXXXXX");
df.applyPattern("#000.#E0");
System.out.println( df.format(12345678)); // 1234.6E4
df.applyPattern("#00.#E0");
System.out.println( df.format(12345678)); // 12.35E6 // how did it decide to place decimal where it did?
// why did it violate my "1 max fractional digit"
// request? I would have expected 123.5E5
df.applyPattern("#0.#E0");
System.out.println( df.format(12345678)); // 12.3E6
// significant digtis = TOTAL int digits + max fractional digits
System.out.println("");
df.applyPattern("###0.#E0");
System.out.println( df.format(12345678)); // 1234.6E4
df.applyPattern("##0.#E0"); // how did it decide to place decimal where it did?
System.out.println( df.format(12345678)); // 12.35E6 // why did it violate my "1 max fractional digit"
// request? I would have expected 123.5E5
System.out.println( df.format(12345678)); // 12.35E6
df.applyPattern("##.#E0");
System.out.println( df.format(12345678)); // 12.3E6
//API example from DecimaFormat RE Scientific Notation.. api says this will print 12.3E3
df.applyPattern("###.##E0");
System.out.println( df.format(12345)); //12.345E3 //violates max fractiona digit request
//NOTE DOCS ARE WRONG ... the number of significant digits is = to max integer digits (number of # and 0 prior
// to decimal point) PLUS max number of digits after the decimal point... NOT Min. Integer digits + Max fractional digits
// suggested pattern
df.applyPattern("000000.##E0");
System.out.println( df.format(12345678)); //123456.78 -
Hi,
I have the following code -
DecimalFormat dnf = new DecimalFormat("0.000E0");
System.out.println(dnf.format(4999888888L));
the output i m getting is 5.000E9
I want the output to be 4.999E9 (i.e. i want do not the rounding off)
how can i do this?
Thanks in advanceHi,
I have the following code -
DecimalFormat dnf = new DecimalFormat("0.000E0");
System.out.println(dnf.format(4999888888L));
the output i m getting is 5.000E9
I want the output to be 4.999E9 (i.e. i want do not
the rounding off)
how can i do this?See
DecimalFormat.setRoundingMode();>
Thanks in advance -
Is there a way to override the way Java turns doubles into strings?
I have code that builds svg documents from scratch and the data can't have exponents in it.
It would be nice not to have to call df.format(x) everywhere while building strings.
DecimalFormat df = new DecimalFormat("0.########");
System.out.println(df.format(8675309.987654321));
double x = 0.0005;
double y = 0.0004;
// this is nice code but doesn't generate useable output
String bla = "(x,y) : (" + x + "," + y + ")";
System.out.println(bla);
// this is ugly code but generates useable output
String hrm = "(x,y) : (" + df.format(x) + "," + df.format(y) + ")";
System.out.println(hrm);
produces...
8675309.98765432
(x,y) : (5.0E-4,4.0E-4)
(x,y) : (0.0005,0.0004)
*/For example, the following code would be huge if I put format statements inside of it.
// from http://www.whizkidtech.redprince.net/bezier/circle/
// this value is used to create control points on bezier curves to make them like circles
public static final double kappa = 4.0 * (Math.sqrt(2.0) - 1.0) / 3.0;
public static String createRoundedRectanglePath(double width, double height, double rx, double ry){
return "M " + rx + ",0.0" +
" L " + (width - rx) + ",0.0" +
" C " + (width - rx + kappa * rx) + ",0.0 " + width + "," + (ry - kappa * ry) + " " + width + "," + ry +
" L " + width + "," + (height - ry) +
" C " + width + "," + (height - ry + kappa * ry) + " " + (width - rx + kappa * rx) + "," + height + " " + (width - rx) + "," + height +
" L " + rx + "," + height +
" C " + (rx - rx * kappa) + "," + height + " 0.0," + (height - ry + kappa * ry) + " 0.0," + (height - ry) +
" L " + "0.0," + ry +
" C " + "0.0," + (ry - kappa * ry) + " " + (rx - kappa * rx) + ",0.0 " + rx + ",0.0" + " Z";
}Thanks,
~EricEric-Frederich wrote:
Is there a way to override the way Java turns doubles into strings?Sadly, it's not "Java" but the classes involved. The way you use it it's StringBuffer's implementation of append() - you won't be able to change that. if you wrote the doubles by themselves instead of concatenating them to a String, you could have tried overriding a stream's write(double d) method.
I haven't used it myself, but maybe the printf() method helps. -
Jformattedtextfield/decimalformat - set number of digits
hi everybody,
i am using a jformattedtextifield and applying a decimalformat with patter "00.00" but when i am setting the value to 1234.1234 its still valid.
i only want to have 12.12 as a valid value or any value with 2 digits befor and 2 digits after the dot.
greets
lukasTry using a MaskFormatter.
-
Please Suggest - new java.text.DecimalFormat Question
table2.setValueAt(
"$"+new java.text.DecimalFormat("#.##").format
((Double.valueOf(table2.getValueAt(row,1).toString())).doubleValue())*
((Double.valueOf(table2.getValueAt(row,2).toString())).doubleValue())
row,
3
);Is there a way to force DecimalFormat to display .00?
Is there a way to ignore a "$" preceding the value from table2.getValueAt(row,1)?Is there a way to force DecimalFormat to display .00?YES: new java.text.DecimalFormat("\u00A40.00").format
-
Java.text.DecimalFormat
hi folks,
I'm trying to format a floating point number so that it has exactly two digits after the decimal
DecimalFormat df=new DecimalFormat("##.00");
System.err.println(df.format(12.995));
My desired output is 12.99(truncation, no rounding)
the actual output is 13.00 (rounding)
how do I acheive what I what?
thank you!!Since DecimalFormat uses ROUND_HALF_EVEN, you need to use something else. The java.math.BigDecimal class is something you could use:
double value = 12.995;
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, BigDecimal.ROUND_DOWN);
System.err.println(bd); This would print out 12.99. -
Java.text.DecimalFormat is parsing '22.3.6' !
I'm using DecimalFormat to mask a data entry field.
When I enter something like 22.3.6, it parses!
String docString = "22.3.6";
NumberFormat nFormat = new DecimalFormat();
float value = nFormat.parse(docString).floatValue();
System.err.println("value: " + value);
Gives me:
value: 22.3
I was expecting a NumberFormatException.
I'm running
build 1.5.0_05-b05
Should I enter this as a bug?I tried yout code and it formats the String the way you wanted:
public class Test
public static void main ( String[] args )
java.text.DecimalFormat df2 = new java.text.DecimalFormat("###########0.00");
System.out.println(df2.format(java.lang.Float.parseFloat("-3.0")));
} //End Main
} -
DecimalFormat bug (?) with scientific notation
Hi there,
i'm currently developing an application dealing with scientific notation of double values.
When i was curious about forcing the DecimalFormat formatter to print an explicit sign character in the exponential part, i found this strange behaviour:
Source code (example generated to show effect):
import java.text.*;
public class DecimalFormatBugTest {
public static void main(String[] arguments) {
DecimalFormat decimalFormat = new DecimalFormat("+0.00000E00");
double testValue1 = 1.23456d;
double testValue2 = 0.98765d;
System.out.println("Test 1: " + testValue1 + " --> " + decimalFormat.format(testValue1));
System.out.println("Test 2: " + testValue2 + " --> " + decimalFormat.format(testValue2));
Output:
Test 1: 1.23456 --> +1,23456E+00
Test 2: 0.98765 --> +9,87650E-+01
This is what i don't understand:
The "workaround" with the explicit "+" in front of the whole format expression is already strange, but when the exponent turns negative, i have output like "E-+00" which is completly senseless.
This output was generated using Java2 1.4.0 @ Win2k.
Thanks for your comments!
Greets, Marvini have no clue why thats behaving that way but i can tell u another workaroun for doing what u want...
I think u got to take that "+" sign off from the format u are giving while constructing the object DecimalFormat. Convert the result of decimalFormat.format(testValue1) into string and check for the character at 0. If its negative then prefix the result with '-' else with '+'. -
DecimalFormat for Double value
hi all,
I have one problem that is
I have Double value like 96.44444 but I want
96.4 only means only one decimal point after the value
so could anyone send the solution...
I think we can solve this problem by using "DecimalFormat" ...
but I don't know how...
thanks
byeeyes, we can...
here goes:
DecimalFormat df=new DecimalFormat("#.#")
to use it:
... df.format(your_value);
Maybe you are looking for
-
Error when starting Adobe reader 11 App-V package
How do I disable Acrobat reader for checking if the ARM Service is present? I have created an app-v 5.0 package for Acrobat reader 11. When I run the package on the sequencer I don't have any issues. When I run it on my test machine I get a pop-up
-
Moving music from old pc to new one
Can someone please help me as I am going out of my mind with frustration here. I am trying to move my Itunes music library from my old pc to my new laptop which is windows vista. Ive an ipod mini, and after following the instructions to the letter fr
-
I have a computer that runs 10.7.4 and can't upgrade to Mavericks because it is pre 2007. Is there anywhere I can download Mainstage 2 safely?
-
The SIM card inserted in this iPhone does not appear to be supported. Only compatible SIM cards from a supported Carrier may be used to activate iPhone. Please insert the SIM card that came with your iPhone or visit a supported Carrier's store to rec
-
A very slow machine (ibook G4)
How do I deal with my extremely slow ibook of late. I am unable to do anything without the beach ball spinning for at least 5 minutes. My hard drive is almost full (that may help) and if this is the reason how do i retrieve space without getting rid