Shop OBEX P1 Docs P2 Docs Learn Events
double to String conversion — Parallax Forums

double to String conversion

PlaneTeeRPlaneTeeR Posts: 100
edited 2006-05-05 17:55 in General Discussion
Hello everyone,

I'm facing a new problem. I have the BPI-216 LCD screen but in my program i'm working with doubles. You can't print Doubles to the LCD with the write functions. So i need to convert them. In PC java it is simple, but when i apply this to the javelin it says:

'Double is either a misplaced package name or non-existent entity'

The method i use is:
str = string to display
i = calculated double

String str = Double.toString(i);

Thanks Johnny
The Netherlands

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-05 11:19
    The only floating point type available for the javelin is Float32,
    which is float, not double.
    The Float32 package has a method print.
    Your alternative is to write a print method for the double type,
    stored in an 8-byte array.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-05 12:20
    Oke so i'm going to use floats.

    But i'm looking at the float32 class and can't find anything with print?

    Remember that i need to have a string or stringbuffer·for the BPI216 LCD!

    Thanks Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-05 12:49
    The Float32.java file from the Float32 package:
    http://www.parallax.com/javelin/underdev.asp

    has method intValue() that converts to an int
    and method toString() that converts a Float32 type to a String type

    So if you have a variable
    Float32 f = new Float32(12); //floating value 12.0

    then f.toString() returns a String with content "12.0" or something like that.
    Check the file for the specific format.
    If you do not like the available format, copy and change the existing toString().

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-05 14:05
    Hey Peter,

    I have this code as simple test code:
    Javelin test code

    import stamp.core.*;
    import stamp.peripheral.display.lcd.serial.BPI216;
    import stamp.math.*;

    public class BPI216Test {
    · final static int LCD_PIN = CPU.pin0;
    · static Uart txOut·· = new Uart( Uart.dirTransmit, LCD_PIN, Uart.invert,
    ································· Uart.speed9600, Uart.stop1);
    · static BPI216 myLCD = new BPI216(txOut);
    · static Float32 s = new Float32();

    · public static void main() {
    ··· s.set("625.0125");
    ··· //System.out.println(s.toString());
    ··· myLCD.CLS();
    ··· myLCD.write(?);
    · }
    }
    You see that I want to display object s, but I can't use the print method of Float32. The BPI216 class has the method write, in this you can send a String but how do i get my string there?

    Thanks Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-05 14:52
    The attachement compiles.

    To write to the LCD, use the same method to make a String as with System.out

    ··· myLCD.write(s.toString());

    That should work.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-05 14:56
    yes it works, why didn't i come up with it tongue.gif !

    Thanks Peter for this, now I can work with the pulse readings again, i didn't finished that yet.

    Johnny·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-05 15:05
    What kind of sensor readings do you do, that you require FP calculations?
    In embedded applications 99% can be done without FP, using only integer math.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-05 15:11
    I don't know yet.

    I've just tried the previous·java code·for real on my BOE·and i got this:

    [noparse][[/noparse]Error IDE-0042] Unknown unhandled exeption in JVM (164).

    And the number is not show on the LCD

    What is this?

    Thanks johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-05 15:20
    The error listing says:
    Unknown unhandled exception in JVM: [font=Arial,Arial size=2]This is an internal error. [/font][font=Arial,Arial size=2]Report error to technical support

    Try the attachment. I added a try/catch.

    regards peter
    [/font]
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-05 15:26
    yes this works, why does it only work with the try/catch?

    thanks Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-05 15:32
    Some java bytecode sequences are not handled by the JVM, hence the Unhandled error.
    My guess is, the try/catch block creates a slightly different bytecode sequence that is handled.
    I have these errors also occasionally. The workaround is usually a try/catch or rewriting
    some small piece of code (for example, changing a for-loop into a while-loop).
    See if you can comment out the catch message. An empty catch block sometimes also works.

    ··· try {
    ····· myLCD.write(s.toString());
    ··· }
    ··· catch (Exception e) {
    ····· //System.out.println("An exception occured during LCD write");
    ··· }

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-05 17:36
    Yes that also works peter!

    thanks for helping me solve this problem!

    Coming back on the sensor readings, i'm making a bike computer and i need accurate readings, is the floating point not necessary on a bikecomputer?

    thanks Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-05 17:55
    Your sensors return integer values I am sure (8bit ADC or 10bit ADC). Considering scaling etc.
    you could use FP. But consider bike speed. Your bike speed is likely
    to be less than 100 km/h so you could let the value 10000 represent 100.00 km/h
    so your bike speed could be known to 2 decimals using integers.
    It is all a matter of interpretation of numbers.

    regards peter
Sign In or Register to comment.