finally! I found a solution to my trouble.
I provide a piece of code:
// This is a sample app which deals with monetary values, money
// so all variables are of integer type ( a basic rule to avoid the unpredictable, fatal rounding of floats' fractional part )
//they are used in pairs to create a price - integer part ( int ) . fractional part ( int ) --->( edit ) WRONG! *
// * ---> It prints incorrectly the less than 10 cents. ( eg. 100 euros and 5 cents = 100.5 --> error )
// FIX: String pointZero = numC < 10 ? ".0" : "." ; ----> 100 euros and 5 cents --> numE + pointZero + numC
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/*
* @author Lifer
*/
public class TheClass extends MIDlet implements CommandListener
{
Form theForm = new Form ( "TheClass" ); // create a Form that contains Items
// for entering the fund information
// initialize the variable at its declaration because
// it has no dependencies from constructor parameters
// all input here
TextField priceE = new TextField( "Mixed Price\nEuros:", "", 7, TextField.NUMERIC ); // numeric, no need of mixed string type
TextField priceC = new TextField( "Cents:", "00", 2, TextField.NUMERIC );
TextField vat = new TextField( "Value Added Tax ( 0 - 100%): ", "", 3, TextField.NUMERIC );
// creation of 2 buttons. one for calculation and one for exit
static final Command calcCommand = new Command ("Calc", Command.OK, 1 );
static final Command exitCommand = new Command ("Exit", Command.EXIT, 1 );
Display display = Display.getDisplay( this ); // addition of a variable storing the Display instance for application
public TheClass () // constructor. it contains all the variables created above
{
theForm.append ( priceE );
theForm.append ( priceC );
theForm.append ( vat );
theForm.addCommand ( calcCommand );
theForm.addCommand ( exitCommand );
theForm.setCommandListener ( this );
}
// create method calc which does all the work
public void calc ()
{
Alert alert = new Alert ("Calc");
int price2E = Integer.parseInt( priceE.getString() ); // for example 100 euros
int price2C = Integer.parseInt( priceC.getString() ); // and 50 cents
int price = price2E * 100 + price2C; // stored in "price" as an integer: 10050
int vat2 = Integer.parseInt( vat.getString() ); // stores the tax, eg 21%
int tax_free = price * 100 / ( 100 + vat2 ); // instead of dividing with 21% ( 0.21 ) and messing
// messing with floats, I "divide" via multiply ( * 100 / 121 )
int tax_freeE = tax_free / 100; // integer part of tax free price ( euros )
int tax_freeC = tax_free % 100; // fractional part ( cents )
String tax_freeP = tax_freeC < 10 ? ".0" : "." ; // EDIT2: fix less-than-10-cents presentation error
alert.setString ( "tax free price: " + tax_freeE + tax_freeP + tax_freeC + " Euros" ); // all output here
// edit1: avoid alert.setString ( "tax free price: " + tax_freeE +"." + tax_freeC ); --> false result when tax_freeC < 10
alert.setTimeout ( Alert.FOREVER ); // the display alert screen stays forever,
display.setCurrent (alert); // and not for limited time unless I press button
}
// actions of our 2 buttons
public void commandAction (Command c, Displayable d)
{
if (c == exitCommand)
{
notifyDestroyed();
}
else if (c == calcCommand)
{
calc ();
}
}
// leave this part as it is
public void startApp()
{
display.setCurrent ( theForm ); // only remember to add correctly the name of the Form variable
}
public void pauseApp()
{}
public void destroyApp(boolean unconditional)
{}
}
This is a sample code of the app i created. Nothing special for pros, yet of great value to me, as I started to become familiar with the MIDlets, their buttons, commands etc etc.
I haven't compiled this piece, if there is sth wrong here, please comment. ( whoever might be interested )