2017-09-04 5 views
1

J'ai quelques produits avec des prix, essayant de calculer une marge et d'envoyer à un autre agent l'offre de prix. Mais, je voudrais le rendre plus lisible, le nombre à virgule flottante est avec trop d'algharismes. Le soufflet de code illustre le cas:Comment formater un nombre flottant dans Jason pour afficher seulement deux décimales?

products([["Banana",1], ["Apple",2], ["Pinapple",2.5]]). 
margin(2). 

//Submit proposal plan, sends to the initiator my product offers 
+!sendProposals[source(X)] : products(List) <- 
.length(List,LLenght); 
?margin(Z); 
-+listSize(0); 
while(listSize(Sz) & Sz < LLenght) 
{   
    .random(Y); 
    .nth(Sz,List,Item); 
    .nth(0,Item,Name); 
    .nth(1,Item,Price); 
    .print("Product(",Sz,"): ",Name," base price $",Price, " calculating..."); 
    //.string.format("%.2f", Price); .format2decimals(Price,X) 
    .send(X,tell,newProposal(Name,Y*Z+Price)); 
    -+listSize(Sz+1); 
}. 

Comment formater comme monnaie avec deux décimales?

Répondre

0

Vous pouvez créer une action interne comme ceci:

import jason.asSemantics.*; 
import jason.asSyntax.*; 

public class formatCurrency extends DefaultInternalAction { 

    private static final long serialVersionUID = 1L; 

    @Override 
    public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { 

     StringTerm result = new StringTermImpl(String.format("%.2f", Float.valueOf(args[0].toString()))); 
     un.unifies(result, args[1]); 

     return true; 
    } 
} 

Dans votre agent, vous pouvez appeler cette action:

package_name.formatCurrency(10.5555,Price);