?? dwprice.java
字號:
/*
* *****************************************************
* Copyright (c) 2005 IIM Lab. All Rights Reserved.
* Created by xuehao at Dec 5, 2005
* Contact: zxuehao@mail.ustc.edu.cn
* *****************************************************
*/
package org.indigo.db;
import java.util.ArrayList;
import java.util.List;
/**
* 對價格數據中的單價進行處理類。
* @author wbz
*
*/
public class DwPrice
{
private static DwPrice itsPrice=new DwPrice();
private DwPrice()
{
}
public static DwPrice getInstance()
{
return itsPrice;
}
/**
* 對單價處理方法。
* @param strPrice
* @return
*/
public String formatPrice( String strPrice )
{
if( strPrice==null )
{
return "";
}
strPrice = strPrice.trim();
List list = new ArrayList();
StringBuffer buf = new StringBuffer();
char []ch = strPrice.toCharArray();
int i=0;
double ptmp=0;
/**
* 循環讀入價格。
*/
for( i=0; i<ch.length; i++ )
{
if( Character.isDigit(ch[i]) || ch[i]=='.' )
buf.append( ch[i] );
else
{
if( buf.length()!=0 )
{
list.add( buf.toString() );
buf = new StringBuffer();
}
}
}
if( i==ch.length )
{
list.add( buf.toString() );
buf = null;
}
String str = null;
double price=0;
/**
* 如果只有一個價格數據則就是所需要的價格。
*/
if( list.size()==1 )
str = (String) list.get( 0 );
else//如果有幾個價格數據,則取平均值。
{
int total=list.size();
for( i=0; i<list.size(); i++ )
{
try
{
ptmp = Double.parseDouble( list.get(i).toString() );
}catch( NumberFormatException e )
{
ptmp = 0;
total--;
}
price += ptmp;
}
price /= total;
str = String.valueOf( price );
}
return str;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -