2017-09-04 69 views
1

我有一些產品的價格,試圖計算一些保證金,併發送給另一個代理的價格報價。但是,我想讓它更具可讀性,浮點數是有太多的algharisms。下面的代碼說明了這種情況:如何格式化Jason中的浮點數以僅顯示兩位小數?

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); 
}. 

如何格式化爲帶兩位小數的貨幣?

回答

0

您可以創建一個內部操作是這樣的:

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; 
    } 
} 

在你的代理,你可以調用這個動作:

package_name.formatCurrency(10.5555,Price); 
相關問題