Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Casting/Conversion Problem

Options
  • 12-09-2001 4:46pm
    #1
    Closed Accounts Posts: 19,777 ✭✭✭✭


    I'm trying to format into a more readable fashion a float by turning it into an int. However, it does not appear to be doing anything at all. Here's the code:
    public static String formatResult(float iRaw) {
      int iTemp = (int)iRaw;
      String sTemp = new String();
      sTemp = Convert.toString(iTemp);
      return sTemp;
    }
    

    I'm using some odd objects/methods and also can't use either double or long data types as it's for a 16-bit platform (PDA). Any suggestions?

    No doubt I'm doing something stupid :o


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Take a look at the classes in the java.text.* package for formatting numbers.

    If you just want to convert a float to a String try this:
    [PHP]
    Float f = new Float(yourFloatValue);
    String floatStr = f.toString();
    [/PHP]


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Originally posted by Enygma
    Take a look at the classes in the java.text.* package for formatting numbers.

    Can't, I'm limited to the classes that come with Waba, which is a cut down library.
    Originally posted by Enygma
    If you just want to convert a float to a String try this:

    I'm already doing that alright, but I'm looking to format the number in a more readable fashion (i.e. 123.45 rather than 1.2345000e02).


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    OK then, the easiest and most portable way out is to mulitply the float by 100 and cast it into an int, which removes anything after two decimal places. Now divide that Integer by 100 and cast the result into a float for printing. (Note no rounding or anything here, I'll leave that up to you, there's an earlier thread on this anyway.)
    [PHP]
    float f =123.345;
    f = f * 100;
    int intVal = (int)f;
    float formattedFloat= (float)intVal / 100;
    System.out.println("formattedFloat is: [" + formattedFloat + "]");
    [/PHP]

    Hope this helps.


  • Registered Users Posts: 16,402 ✭✭✭✭Trojan


    Perhaps you want to do a check on size just before you multiply by 100 there, or is that unnessary in Java?

    Al.


  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    Well strictly speaking you should, but the largest positive float possible is 3.40282347e+38, and the smallest is 1.40239846e-45F, so unless you're working with inordinately large numbers you don't need to.


  • Advertisement
  • Registered Users Posts: 16,402 ✭✭✭✭Trojan


    Originally posted by Jazz
    Well strictly speaking you should, but the largest positive float possible is 3.40282347e+38, and the smallest is 1.40239846e-45F, so unless you're working with inordinately large numbers you don't need to.

    Always check for boundary conditions, they're always the gotcha :)

    Al.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I felt lucky in google and came up with this. The Rounding Class isn't mine:
    iimport java.lang.*;
    import java.math.*;
    
    public class ****{
    
     public static void main(String[] args)
    	{
    		float i;
    		i = 289.988f;
    		System.out.println(formatResult(i));
    	}
    
     public static String formatResult(float iRaw)
    	{
    	      	double d;
                    int i;
    		Float f= new Float(iRaw);
                    d = f.doubleValue();
                    i = 4;
    
    		String s = new String(Rounding.toString(d, i));
    	      	return s;
    	  }
    
    }
    
    class Rounding
      {
        public static String toString (double d, int place)
        {
          if (place <= 0)
            return ""+(int)(d+((d > 0)? 0.5 : -0.5));
          String s = "";
          if (d < 0)
            {
      	s += "-";
      	d = -d;
            }
          d += 0.5*Math.pow(10,-place);
          if (d > 1)
            {
      	int i = (int)d;
      	s += i;
      	d -= i;
            }
          else
            s += "0";
          if (d > 0)
            {
      	d += 1.0;
      	String f = ""+(int)(d*Math.pow(10,place));
      	s += "."+f.substring(1);
            }
          return s;
        }
    }
        
    

    It returnes 289.9880. I hope this works with the reduced JVM.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Originally posted by Enygma
    OK then, the easiest and most portable way out is to mulitply the float by 100 and cast it into an int, which removes anything after two decimal places. Now divide that Integer by 100 and cast the result into a float for printing.

    Unfortunately, that resulted in numbers being formatted again as the original float - so back to square one. The solution I found was a filthy dirty hack that fomatted the number once is was a string:

    [PHP]
    public static String formatResult(float iRaw) {
    String sTemp = new String();

    iRaw = iRaw * 100;
    int iTemp = (int)iRaw;
    sTemp = Convert.toString(iTemp);
    if (iTemp > 0) {
    sTemp = sTemp.substring(0, sTemp.length() - 2) + "."
    + sTemp.substring(sTemp.length() - 2, sTemp.length());
    if (iTemp < 100) sTemp = "0" + sTemp;
    } else {
    sTemp = "0.00";
    }
    return sTemp;
    }
    [/PHP]

    Not Pretty, but it appears works (debugging will come later :rolleyes: ).
    Originally posted by Trojan
    Always check for boundary conditions, they're always the gotcha :)

    That's done prior to both the formatting and the core calculation in a seperate method.

    Thanx all for the pointers.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Originally posted by Evil Phil
    I felt lucky in google and came up with this.

    Thankx m8, but as from my previous post I solved it already. Anyhow, I could (just glancing at the code) see problems with it right away.

    For example, it makes use of doubles, which I can't use as it's running on a 16-bit machine and they require 32-bit. Also, while I did find a custom Math class, it does not come with the default Waba libraries.

    Anyhow thanx again.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Oh yeah, I've just re-read your initial post (doh!).


  • Advertisement
  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    I havent tried running it, but your initial code actually looks solid.

    I would be inclined to guess that this is serious breakage in the maths lib you are using...or in the Java implementation, as it should be technically impossible for a primitive int to contain decimal spaces....the basic storage mechanism in memory alone should prevent it.

    So, allowing for that, I would say that your workaround is probably the "right" solution.

    jc


  • Registered Users Posts: 380 ✭✭dogs


    Heya,

    Sorry to nit-pick, but am I missing something here...

    [PHP]
    String sTemp = new String();
    sTemp = Convert.toString(iTemp);
    [/PHP]

    ...why do you create a new String() object and then
    immediatley dereference it ? Convert.toString() is returning
    a reference to a String anyway...

    am i just dumb ? :)


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Originally posted by dogs
    Sorry to nit-pick, but am I missing something here...

    Yes you're nit-picking. Would you prefer:

    [PHP]
    String sTemp = Convert.toString(iTemp);
    [/PHP]


Advertisement