setText and String.format not working together

Put your problem here if it does not fit any of the other categories.

setText and String.format not working together

Postby sandman42 » Fri Apr 23, 2010 5:40 pm

Hi,

consider the following snippet:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. float myTestFloat;
  2.  
  3. String myTestString;
  4.  
  5.  
  6.  
  7. myFloat = 100f;
  8.  
  9. myTestString = String.format("%3.0f", myFloat * 3.6f)
  10.  
  11.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

why

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. textView01.setText("Myfloat is: " + myTestString);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


works, i.e. the textview is updated with "MyFloat is: 360" wile

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. textView01.setText("Myfloat is: " + String.format("%3.0f", myFloat * 3.6f));
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


doesn't work, i.e. it returns "MyFloat is 0"??????
:o :?

Ciao
sandman42
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 23, 2010 5:26 pm

Top

Postby uncre8d1 » Thu Apr 29, 2010 3:45 pm

I'm also having problems with string.format; I can't seem to set the precision ("%10.2f", number) without the program crashing. If I just use "%f" it works but I get the default 6-place precision.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                                         //------Normalize percentage value-----------
  2.  
  3.                                         if (tippercent > 1)             {tippercent = tippercent / 100;}
  4.  
  5.                                        
  6.  
  7.                                         double billtotal = (checktotal+(checktotal * tippercent));
  8.  
  9.                                         double tipdtotal =(checktotal * tippercent);
  10.  
  11.                                         double percost = ((checktotal+(checktotal * tippercent))/numberofppl);
  12.  
  13.                                         double pertip = ((checktotal * tippercent)/numberofppl);
  14.  
  15.                                        
  16.  
  17.                                
  18.  
  19.                                
  20.  
  21.                                 totbill.setText(String.format("%f", billtotal));
  22.  
  23.                                
  24.  
  25.                                 tottip.setText(String.format("%<10.2f", tipdtotal)); //This crashes the app
  26.  
  27.                                
  28.  
  29.                                 costper.setText(String.format("%f", percost));
  30.  
  31.                                
  32.  
  33.                                 tipper.setText(String.format("%f", pertip));
  34.  
  35.  
  36.  
  37.                                
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Normal?!? Man, I'd give my left arms to be normal.
User avatar
uncre8d1
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Mar 16, 2010 3:41 am
Location: Silver Spring, MD

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Alexa [Bot] and 9 guests