String to double

hi,
I'm trying to read spectrum data of an spectrum analyzer and using VISA commands.
I can get trace data as a string in ASCII format in a form in which each double elements are comma-separated.
How can I format this stringto a double array?
thanks a lot

Maybe this can help :
// Split data string into points, convet to double, and output
token = strtok( data, "," );
dataPoint = atof(token); // Convert to double
you can also use Fmt() and Sprintf() for manipulate strings
Kobi Kalif
Software Engineer

Similar Messages

  • Convert a string to double with precision

    I am getting a value amount of say $45.00 from a jsp and set this value as a string for my other work. then i am changing this string to double by using this statement
    setAmount(Double.parseDouble(cstmt.getString(1)));
    my precision is lost when i get this amount it just shows 45.0 how can I get the precision of 45.00
    I would appreciate if any assistance is provided for the problem.
    thanks

    JSP's can use NumberFormat.
    However you should never use float/double to represent monetary values. Money has an absolute value (in terms of units used to purchase). Float and double do not (actually they do but they are in base 2 NOT base 10 like most money is). In most cases representation errors due to float and double do not really matter (if you have suitably analysed the usage of them) however people get very upset about errors when it comes to money
    matfud.

  • StringTokenizer class problem with strings in double quotes

    Hello Technocrats,
    I have a problem with tokenizing following string enclosed in (). (abc," India, Asia", computer engineer). My separator is ",", thus StringTokenizer class gives me 4 tokens namely abc, "India, Asia" and computer engineer. But I require that String in double quotes should be a single token. How to achieve this using StringTokenizer class? Or is there any other way?
    Thanks in advance.

    Try
    String[] str="abc,\" India, Asia\",computer engineer".split(",",1);
              for(String s: str)
                   System.out.println(s);
              }Thanks.

  • 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 double

    cupofjava666 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

  • How to change String to double ?

    I want to change String to double in Applet program, but for the run time, error is occurred.
    It can not find the method parseDouble.
    Error -> Double.parseDouble("7.5");
    I try to test String to Integer, it works fine.
    OK -> Integer.parseInt("7");
    How to change String to double in Applet ?

    Yea, the Double.parseDouble method came in a later release (first time in 1.2).

  • Convert String to double with two digits

    How can I format "String" type of numbers (eg. 199.0, or 199) to return a "double" type of nubmers like 199.00, or 199.00. It should have two digits.
    For example, the input is String of 199.0, how can I get "double" 199.00. Always format as two digits.
    Thanks for your help,

    Thanks for replying.
    Here is my code. For some reason, when convert the returned String back to double in main, it cut the "0".
    What's wrong with my code? How can I get that 4.60 in double type.
    Thanks again,
    ==================================================================================
    import java.text.*;
    import java.util.*;
    import java.sql.*;
    public class NumUtils
    // Convert Double to 99.99
    public static String format(String str)
    //double d = 4.6;
    double d = Double.parseDouble(str);
    NumberFormat formatter = new DecimalFormat("0.00");
    formatter.setMaximumFractionDigits(2);
    String s = formatter.format(d);
    return s;
    public static void main(String[] args)
    String myDouble = "4.6";
    String mynewdouble = format(myDouble);
    System.out.println(" --- formatDouble = " + mynewdouble);
    double my_d = Double.parseDouble(mynewdouble);
    System.out.println(" --- Double = " + my_d);

  • Parse a line of string to double

    Hi all:
    I have a file that contains a matrix of double values, and I need to parse each of this value and store them in double format. Each line in the file contains the same number of values, and each value within a line is seperated by a space character. Currently, what I am thinking is first read in a line, then use regular expression to find matches one by one, then parse each of the matched string to double using Double.valueof(String string). I wonder is there any better method to do this?

    Use String.split() with a whitespace regex, and then parse each result.

  • Help needed (converting string to double)

    keep getting an error someone give me a hint
    public class q1d
        public static void main(String [] args)
            String whats;
            double x;
            x = (String)whats;
            System.out.println(+x);
    }

    Then study this tutorial
    http://java.sun.com/docs/books/tutorial/
    Specifically, this part
    http://java.sun.com/docs/books/tutorial/java/data/index.html
    You need some basic knowledge. These forums are not suitable for teaching Java. The tutorial is.

  • Splitting CSV file; output a simple tokenised string of doubles - HELP

    Okay here it goes? I have a file that contains comma delimited double values in the form;
    2,3
    2.8,9.0
    4.8,9.0
    I am trying to get the values in to the a simple string of "2 3 2.8 9.0 4.8 9.0". Therefore i have tried to replace all commas with a space and then split on whitespace? no success there?
    Th other way is to split on white space string.split("\\s+") and creat a string [] array and then loop through each element of the array creating a multi dimensional array of seperated values. These i could then concatente to form a string?
    My question is where is my code going wrong for spliting the string???
    package LineGraph;
    import java.util.*;
    import java.io.*;
    public class ReadData extends Thread {
         private Cdf cdf = null;
         private String fileName = null;
         String rawData = "";
         double[][] data = creatData();
         static double[][] creatData() {
              String rawData = GUI.jta.getText();
                System.out.println("rawData: " + rawData);
                String dataSet = rawData.replaceAll(",","\\s+"); 
                String [] point = null;
                point = rawData.split("\\s+");
                for (int i=0; i < point.length; i++){
                     System.out.println("point:" + point);
              //for (int i = 0; i < dataSet.length; i++){
              //System.out.println("dataSet: " + dataSet[i]);
              //String [] point = null;
              //if (dataSet.length > 0) {
              // point = dataSet[0]; // start with the first element
              // for (int i=1; i<dataSet.length; i++) {
              // point = dataSet[i];
              //System.out.println("point:" + point);
              //String[] CoOrd = point.split(",");
              String result = "";
              if (point.length > 0) {
                   result = point[0];
                   for (int j=1; j<point.length; j++) {
                        result = point[j];
              StringTokenizer tokens = new StringTokenizer(result);
              List list = new LinkedList();
              while (tokens.hasMoreElements()){
                   String number = "";
                   String token = tokens.nextToken();
                   for (int i=0; i<token.length(); i++){
                        if (Character.isDefined(token.charAt(i))){
                             number += token.substring(i, i+1);
              if (!number.equalsIgnoreCase("")){
                   boolean add = list.add(number);
         System.out.println("list:" + list);
              double [][]data = new double[list.size()/2][2];
              int index = -2;
              for (int i=0; i<data.length;i++){
                        index += 2;
                        data[i][0] = Double.parseDouble(
                                  (list.get(index).toString()));
                        data[i][1] = Double.parseDouble(
                                  (list.get(index +1).toString()));
              System.out.println("data.length: " + data.length); //** add this -- see how much data is being created
              return data;
              //cdf = new Cdf(data, fileName, PrintMessage);
         public Cdf getCdf(){
              return this.cdf;

    the point being there are 2 delimiters to split, the first being the comma the second being the line break thus creating an array of single digits
    I have solved the issue by using replaceall to replace all commas with spaces and then split on the spaceStill makes no sense. A line break doesn't not contain spaces. So how does replacing commas with spaces allow you to split on the line break.
    If you are appending each line to a string (to build one long string) then append the data with a comma, not a space.
    I really get the idea you guys are not enjoying my code?Its your requirements we don't understand. You obviously aren't explaining them correctly.

  • Strange outputs while parsing String to Double

    Hi All,
    I am writing a simple MIDlet on MIDP 2.0 and CLDC 1.1, where I need to convert two strings into Double calculate their difference and then display the result back on the screen.
    The code I am using is:
        double difference = Double.parseDouble("1235.7") - Double.parseDouble("1234.5");
        String result = (String)Double.toString(difference); The output generated is : 1.2000000000000455 which should be only 1.2
    If I replace the values with "1235" and "1234.1" the output is 0.900000000000091 which should be 0.9
    However, if I change the values to "1235.6" and "1234.1" the output generated is 1.5 which is perfectly fine. Means this happens only for some specific set of values.
    Has anyone ever faced such a situation or if anyone knows a proper workaround to fix this please help me out.
    Thanks.

    Is this normal? does this also happens in case of J2SE I have never tried a similar
    program on j2se.If you are talking about the innaccuracies in the calculations then yes. Look at the url I gave in my previous post, some guy was asking the same thing and it wasn't about J2ME.
    In fact, this happens in every other language as well. It a limitation of the format doubles are stored in.
    shmoove

  • Cast string to double? how?

    I have a JTextField in swing that takes a string. I have a method that takes a double.
    premise: User enters 2.0 and I feed it to the method.
    problem: type mismatch
    I just tried myParam = (double) myTextInput; but the javac didnt like it.
    Is there a toDouble()? I looked in the API under String and Double and didnt see one.

    Here are a few examples of convesions
    http://www.javagalaxy.com:8080/CoreJava/View.jsp?slno=1&tbl=0

  • Converting from string to double (formula)

    Hi All,
    I have a formula stored in properties file (i am doing some android dev) and retrieveing that formula using following stmt
    double c = 35.0;
    String res = this.getResources().getText(R.string.celsiusfahrenheit).toString();
    The returned value of string res is (1.8*c)+32
    How do i make the above stmt to substitue value of c into above equation and returned me the result value.
    thx
    vinod

    vinod_kmr wrote:
    Hi All,
    I have a formula stored in properties file (i am doing some android dev) and retrieveing that formula using following stmt
    double c = 35.0;
    String res = this.getResources().getText(R.string.celsiusfahrenheit).toString();
    The returned value of string res is (1.8*c)+32
    How do i make the above stmt to substitue value of c into above equation and returned me the result value.
    thx
    vinodas I understand you actually want to solve. if so, and res always returns that formula, then you can skip the properties file and just solve it already. something like:
    double result = (1.8*c) + 32.0if the result you are looking for is res with the value of the double substituted for c in the formula then the first step is to convert the the double c into a string and sticking that in res using String.replace(). something like
    res = res.replace("c", Double.toString(c));of course I don't know if String in android is like java.lang.String (same with Double) but I'm sure an equivalent exist.

  • How to convert a string into double?

    Hi. I'm having a string with 10340.8878 which is essentially a GPS latitude coordinate. I will need to break it up separately into double type 103,40 and 88.
    103 refers to the degree
    40 refers to the minutes
    88 refers to the seconds
    I will also need to perform a double conversion: the second separated double(40) divided by a factor of 60 and the third separated double(88) divided by a factor of 360.
    How can i go about doing this?
    Thanks!

    new Double(str).doubleValue();
    http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Double.html

  • Need to know the best practice for converting string to double

    I have a string and want to convert to double if it is a valid number, else want to keep as it is. There can be couple of ways doing it and I want to know which one is best if I have lots of strings, specially from performance point of view.
    1) Use Double.parseDouble(myString) and catching Number format exception to detect it is not a number. One of my colleague said it does not give good performance because of exception catching,
    2) Use of org.apache.commons.lang.math.NumberUtils.isNumber() and if it is true then only parse it - so don't rely on exception.
    I did some performance testing - putting it in a loop and trying out for 2 scenarios - one loop for proper numeric value string and another for non-numeric. What I found out was if strings are not proper then parseDouble() is taking long time (because of exception catching) and in that case using NumberUtils.isNumber() makes sense.
    Would like to hear expert views on this.
    Thanks
    Manisha

    If you need it as a double you must convert it to a double and catch the exception. This means that testing it first is a waste of time in the case when the test succeeds - did your colleague think of that?
    Catching the exception is possibly slower than the test. Whether this is significant depends on the relative timings of the test and catching the exception, and also on the expected error rate. If this is below about 40% I suggest your colleague is talking through his esteemed hat.
    And of course the best test by far is the conversion itself. Using any other test runs the risk of its rules being different from those applied by the conversion.
    In any case you are obliged to write the code that catches the exception. You're not obliged to write the pre-test code.
    My personal rule for efficiency is to minimize lines of code until hard evidence to the contrary proves that further improvement is required.

  • LV RT - String to double conversion

    Hello,
    I want to convert a string to a double on my Real Time device (PXI-8101. When I use the "scan value" or "Fract/Exp String To Number Function" VI the numbers after comma dissapear. This does not happen on the Host (PC). 
    So for example on the host vi, the string "2121,3" gives the number 2121,3. If I do the same thing on the Real Time, the string "2121,3" returns the number 2121. 
    How can I fix this?
    Thanks in advance,
    Regards,
    Dries
    Solved!
    Go to Solution.
    Attachments:
    String2Number.vi ‏7 KB

    Hello,
    Thanks for the reply, but scan from string also does not return the correct number on the RT device. The VI returns the number after comma as the remaining string. Any other suggestions?
    Best regards,
    Dries
    Attachments:
    String2Number.vi ‏8 KB

Maybe you are looking for