converting Double to string

string() doesn't convert a double to a string in the string manipulator node.

When I try use a Double.ToString in a Java Snippet it gives me a non-recurring decimal. When I use a groupby and concatenate it does the same. Is there anyway around this?

 

Thank you

Varun

Can you simply use the number to string node instead?

simon,

If you are getting long numbers of chracters after the decimal point, try using DecimalFormat or String.format to control the output in a Java Snippet.

Following links show how to use them
E.g.

or

http://examples.javacodegeeks.com/core-java/lang/string/java-string-format-example/ (See example 3 for String.format(…) syntax

Steve

Put this into the string manipulation node

string(toLong($YOUR_COLUMN$))

 

Cheers

Hi All,

@richards99: I tried Number to String and the problem still prevails: 0.622 -> 0.62192744…
@andyg: Unfortunately when I try that it just returns the integer value.
@s.roughley: I haven’t programmed in Java ever and am unfamiliar with the language. However I did try

“”“
public class StringFormatExample{
public static void main(String[] args) {
System.out.printf(“Floating point number with 3 decimal digits: %.3f\n”,$COLUMN_NAME$);
}
}
”""
and it throws errors in a Java Snippet(simple) node.

Thank you for the replies everyone.

varun

Hi,

Your cell must contain a recurring decimal to start with, its probably that when the number is rendered on-screen, its automatically rounded off. Therefore;

Before using the Number to String node, use the Round Number node, and specify how decimal points or significant figures you wish your number to contain.

Simon.

Thank you Simon. That worked!

Top marks to Simon for my most-overlooked Round Number node.  For reference, in the Java Snippet(Simple) you need to set your return type to String, and then enter code something like:

Double dbl = $DblColumnName$;  //Select by double clicking your double column to get the format correct after the = sign

String r = String.format ("%.3f", dbl);
return r;

Steve

PS - The Java Snippet (Simple) node was where I first learnt to write Java!