Conversion of Double to String
Is there any method in JAVA which can convert a double value to String (but expand it)
for ex: I want to convert 1.2E7 to look like "12000000"
I know an equal C conversion technique which is as follows => sprintf(strAmount, "%35.2lf", fAmount);
Plz.. Reply to:
[email protected]
i have tried the DecimalFormat in the reply, but the d
in the fd.format(d) is still in xxxxxxxxEx format. And
i couldn't perform any calculation based on the
converted value with this format after saving this
value to may be database or some other files. Can you
advice how to get 170000000.00 instead of
1.70000000E8.Use the setMaximumIntegerDigits() method of the NumberFormat.
Servus.
Similar Messages
-
I'm having a problem in getting my double to convert to a string, here's my code so far.
public void calculatePayments()
double paymentAmount;
double monthlyInterestRate = (Double.parseDouble(interestBox.getText())/12)/100;
double amountOfLoan = Double.parseDouble(balanceBox.getText());
double numberOfPayments = Double.parseDouble(yearsBox.getText()) * 12;
paymentAmount = (amountOfLoan * monthlyInterestRate) / (1 - Math.pow(1/(1 + monthlyInterestRate),numberOfPayments));
Double.toString(paymentAmount);
tableBox.append(paymentAmount);
And here's the error message I get:
ProgramInterface.java:134: append(java.lang.String) in javax.swing.JTextArea cannot be applied to (double)
tableBox.append(paymentAmount);
As always, help is appreciated.This line returns a String but has no affect on paymentAmount.
Double.toString(paymentAmount);To covert the double to String you can save the return in a variable
String s = Double.toString(paymentAmount);
tableBox.append(s);or combine them
tableBox.append(Double.toString(paymentAmount)); -
I am looking for a way to convert double to String.
I think that I have to use wrapper like Double class and use toString(double d), but I am not sure how to do it.
Since toString() is static method, I do not have to have an instance of the Double class, so I was wondering what's the correct way to do it.
I need something like this:
String MyString = new String (String1.concat ( My_doubleNumber.Double.toString() ) );
Any Suggestions?
Thanks!!!WOW thanks a lot. These are definately better than what I thought of doing originally.
In case I need to use the wrapper class somewhere else, I am just wondering what would be a proper use of Double and String? -
Converting Double to String without exponent
Hi all,
I am reading some values from Excelsheet using apache POI library. I have large numbers that need to stored into database as string and not numbers. hence, while reading the excelsheet values, i convert these number.. and the are received as double. While converting double into string I noticed that some values are represented in exponential form.. I would like to avoid this and have pure number values in the form of string,,..
Can anybody tell me how to do it?
Thanks in advance,
Abdel Olakarajava.text.DecimalFormat
-
Conversion from document to string
I want to perform conversion from document to string for tht purpose i m doing sax parser but in the end i m getting null.The same thing if i do with DOM parser its working fine but some problem with sax parser.
I am attaching the code if anyone can find the problem it would be gr8.
Thanks in advance.
public String DocumentToString(Document doc) {
StreamResult result = null;
try {
SAXParserFactory SAXpf = SAXParserFactory.newInstance();
SAXParser SAXparser = SAXpf.newSAXParser();
XMLr = SAXparser.getXMLReader();
Source sXML = new SAXSource((InputSource) doc);
result = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(sXML, result);
catch (TransformerConfigurationException e) {
e.printStackTrace();
catch (TransformerException e) {
e.printStackTrace();
catch(Exception e)
System.out.println(e);
return result.getWriter().toString();
}Paddy,
There are a couple of ways to create a Word file. One is to use the Report Generation Toolkit which includes vi's to create and edit Word documents. Since you are associated with a university you may already have the toolkit. The other way is to use ActiveX. You should be able to find examples of both in the forums. There may also be an example that shipped with LV.
Here is one example to get you going http://zone.ni.com/devzone/cda/epd/p/id/992 -
Double.parseDouble(String) - problems when string is in scientific notation
Hello guys,
I'm doing some numerical calculations and I wonder whether it is possible for Double.parseDouble(String) to parse string in the scientific notation i.e. 1.0824234234E-10. Is it the notation itself causing the exception : NumberFormatException or the number is just too big/small and double can't hold it ?
If it's just the notation how can I fix it ?
Regardsi'm not quite sure whether double odoes not allow it.
perhaps consider the api Double.valueOf() and the testing code provided; reproduced below:To avoid calling this method on a invalid string and having a NumberFormatException be thrown, the regular expression below can be used to screen the input string:
final String Digits = "(\\p{Digit}+)";
final String HexDigits = "(\\p{XDigit}+)";
// an exponent is 'e' or 'E' followed by an optionally
// signed decimal integer.
final String Exp = "[eE][+-]?"+Digits;
final String fpRegex =
("[\\x00-\\x20]*"+ // Optional leading "whitespace"
"[+-]?(" + // Optional sign character
"NaN|" + // "NaN" string
"Infinity|" + // "Infinity" string
// A decimal floating-point string representing a finite positive
// number without a leading sign has at most five basic pieces:
// Digits . Digits ExponentPart FloatTypeSuffix
// Since this method allows integer-only strings as input
// in addition to strings of floating-point literals, the
// two sub-patterns below are simplifications of the grammar
// productions from the Java Language Specification, 2nd
// edition, section 3.10.2.
// Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
"((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
// . Digits ExponentPart_opt FloatTypeSuffix_opt
"(\\.("+Digits+")("+Exp+")?)|"+
// Hexadecimal strings
"((" +
// 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "(\\.)?)|" +
// 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
")[pP][+-]?" + Digits + "))" +
"[fFdD]?))" +
"[\\x00-\\x20]*");// Optional trailing "whitespace"
if (Pattern.matches(fpRegex, myString))
Double.valueOf(myString); // Will not throw NumberFormatException
else {
// Perform suitable alternative action
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html -
Double.parseDouble(String) : Problem to parse large String
Hello,
I need to convert A String to a Double in my application without rounding of digits.
I have used the Double.parseDouble(String) method.
The maximum string length can be 17 including dot(.)
So if my String is (of length 17)
Eg.
S= �9999999999.999999� then I am getting the double value as
d= 9999999999.999998(compare the last digit)
If s = �9999999999.111111� then d = 9999999999.111110
If s = �9999999999.555555� then d = 9999999999.555555 (result that I want)
If s = �9999999999.666666� then d = 9999999999.666666 (result that I want)
If s = �9999999999.777777� then d = 9999999999.777777 (result that I want)
If s = �9999999999.888888� then d = 9999999999.888887
If s = �9999999999.999999� then d = 9999999999.999998
If s = �9123456789.123456� then d = 9123456789.123456
But string length up to 16 is giving me the accurate result
So any body can explain me that why it is happening? And how can i get the accurate result.
Thanks in advanced.Hi,
Thank You for your suggestion. By which i can store
the big double number in Data base , but at some
point i require to parse a double value from a Double
reference.In that case if the Decimal value length
=> 16 then it the last digit changes or rounds.
coverting 9999999999.999999 Decimal value to double
value. It gives me 9999999999.999998 and
9999999999.9999999 gives me the 10000000000.0000000.
but the Double value 999999999.999999 is ok
and 999999999.99999999 rounds.Err, is there a follow up question in there? -
Kinda urgent please help pass strings to doubles and strings to ints
Need to know how to pass strings to doubles and strings to ints
and to check if a string is null its just if (name == null;) which means black right?
like size as a string and then make the string size a doublecupofjava666 wrote:
Need to know how to pass strings to doubles and strings to ints
and to check if a string is null its just if (name == null;) which means black right?
like size as a string and then make the string size a doubleThink he means blank.
Check the Wrapper classes (Double, Integer) in the api.
parseInt() parseDouble() both take a string and return a primitive.
String s = null;
if(s == null) should do the trick!
Regards.
Edited by: Boeing-737 on May 29, 2008 11:08 AM -
Printing out double quote string
Hello,
I would like to know how can i print out the double quote string (")
in JSP using out.print(), because a double qoute has already used
for indicating the string. If the string that i want to print out is including double quote("), how can i print that string out.
THX !out.println("\"mystring\"");
generally: backslash "\" is used in Java to quote special characters e.g. out.println("c:\\my documents\\bla"); -
How do you convert a double to a String. Can't seem to find out how.
Thanksdouble d = 2.2;
String dStr = ""+d;
or
dStr=Double.toString(d); -
SSRS Report Returning Double Quote string from a Single Quote String
Hi, I'm getting weird thing in resultset from SSRS report when executed. When I pass parameter to a report, which passes String that has single quote value to a split function , it returns rows with double quote.
For example following string:
'N gage, Wash 'n Curl,Murray's, Don't-B-Bald
Returns:
''N gage, Wash ''n Curl,Murray''s, Don''t-B-Bald
through SSRS report.
Here is the split function Im using in a report.
CREATE Function [dbo].[fnSplit] (
@List varchar(8000),
@Delimiter char(1)
Returns @Temp1 Table (
ItemId int Identity(1, 1) NOT NULL PRIMARY KEY ,
Item varchar(8000) NULL
As
Begin
Declare @item varchar(4000),
@iPos int
Set @Delimiter = ISNULL(@Delimiter, ';' )
Set @List = RTrim(LTrim(@List))
-- check for final delimiter
If Right( @List, 1 ) <> @Delimiter -- append final delimiter
Select @List = @List + @Delimiter -- get position of first element
Select @iPos = Charindex( @Delimiter, @List, 1 )
While @iPos > 0
Begin
-- get item
Select @item = LTrim( RTrim( Substring( @List, 1, @iPos -1 ) ) )
If @@ERROR <> 0 Break -- remove item form list
Select @List = Substring( @List, @iPos + 1, Len(@List) - @iPos + 1 )
If @@ERROR <> 0 Break -- insert item
Insert @Temp1 Values( @item ) If @@ERROR <> 0 Break
-- get position pf next item
Select @iPos = Charindex( @Delimiter, @List, 1 )
If @@ERROR <> 0 Break
End
Return
End
FYI: I'm getting @List value from a table and passing it as a string to split function.
Any help would be appreciated!
ZKAnother user from TSQL forum posted this code which is returning the same resultset but when I execute both codes in SQL server it works and return single quote as expected.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/8d5c96f5-c498-4f43-b2fb-284b0e83b205/passing-string-which-has-single-quote-rowvalue-to-a-function-returns-double-quoate?forum=transactsql
CREATE FUNCTION dbo.splitter(@string VARCHAR(MAX), @delim CHAR(1))
RETURNS @result TABLE (id INT IDENTITY, value VARCHAR(MAX))
AS
BEGIN
WHILE CHARINDEX(@delim,@string) > 0
BEGIN
INSERT INTO @result (value) VALUES (LEFT(@string,CHARINDEX(@delim,@string)-1))
SET @string = RIGHT(@string,LEN(@string)-CHARINDEX(@delim,@string))
END
INSERT INTO @result (value) VALUES (@string)
RETURN
END
GO
ZK -
Conversion of XSTRING to String is incomplete
Hi ,
I am trying to conver a Xstring to String. But the output string after conversion is incomplete.
Here is the code snippet-
CALL METHOD CL_ABAP_CONV_IN_CE=>CREATE
EXPORTING
INPUT = l_xstring
ENCODING = 'UTF-8'
REPLACEMENT = '?'
IGNORE_CERR = ABAP_TRUE
RECEIVING
CONV = loc_CONV.
length = xstrlen( l_xstring ).
CALL METHOD loc_CONV->READ
EXPORTING
N = length
IMPORTING
DATA = loc_string
LEN = length.
Can anyone please help?
Best Regards,
LataHi,
http://www.sapfans.com/forums/viewtopic.php?p=403405&sid=e524cddae19e7ae48a12477e68c6032d
Regards,
Sravanthi -
How can I do that?
Double.toString(double);
String e = "" +double;
Is this what you are looking for? -
Double to String with 2 characters decimals
How to a get a String from a Double with only 2 characters after the dot/comma ?
chmurbdouble aDbl = XXXXX;
DecimalFormat fmt = new DecimalFormat( "0.00;-0.00"
String stringVal = fmt.format( aDbl );^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
a better answer than mine :) -
i write such code
double a = 4.3
double b = 3
System.out.println(a*b)
why return value is 12.8999999999999
how to make it return right string?Please help me!Computers can't accurately handle certain floatingpoint values, that's a fact.
Use the DecimalFormat class to format the string to having a certain amount of digits after the decimal point, that should give you the correct value (or at least a nicer looking one).
Maybe you are looking for
-
HT204370 I bought a hd movie and now it won't play on my computer
It started to play and then said it couldn't be played in HD. Asked to do SD. When I press play, it is a black screen .
-
Java Applet and MS SQL 7.0
I wrote a Java application that connects to a local MS SQL 7.0. It works just fine. But when I converted it to a simple Java applet, then I connected to the database, an error occured: "java.security.AccessControlException:access denied(java.lang.Run
-
Jco.JCO$Exception when create a new adaptive model in NWDS
HI friends: when I create a new adaptive model in NWDS, error occurs: Warning:Creating a connection with Metamodel language <zh_CN> failed. Continuing with language <zh> Fatal: com.sap.mw.jco.JCO$Exception: Missing R3NAME=... or ASHOST=... in connect
-
I am new to j2ee and as per the j2ee tutorials 1. I have installed j2ee sdk, 2. installed jdk 1.3.1 3. installed ant 4. set JAVA_HOME 5. set J2EE_HOME 6. set ANT_HOME, but when I try to start the j2ee server on my command prompt, I am geting follwoin
-
How can I get rid of desktop icon without deleting the actual item?
Installed Seagate as time capsule. Icon on desktop. How can I get rid of icon from desktop without deleting the Seagate?