📄 convert.java
字号:
}
/**
*Convert variant to date
*/
public static HDate var2date(Variant val) throws HpException
{
if(val.getType() ==Variant.V_NULL)
throw new HpException(94,"Invalid use of Null");
return new HDate(val.dblValue());
}
/**
*Convert variant to currency
*/
public static HCurr var2curr(Variant val) throws HpException
{
if(val.getType() ==Variant.V_NULL)
throw new HpException(94,"Invalid use of Null");
return new HCurr(val.dblValue());
}
/**
* Convert variant to string
*/
public static String var2str(Variant val) throws HpException
{
if(val.getType() == Variant.V_NULL)
throw new HpException(94,"Invalid use of Null");
return val.strValue();
}
public static String obj2str(Object var ) throws HpException
{
return var.toString();
}
/**
*Returns a string representation of a double
**/
public static String dbl2datestr(double date) throws HpException
{
int days;
String date_str = null;
String time_str = null;
String DateString = null;
double times;
days = (int)date;
times = Math.abs(date - days);
if(days != 0)
date_str = DaysString(days);
else
if(times == 0)
date_str = "12:00:00 AM";
if(times != 0)
{
time_str = TimesString(times);
if(date_str != null)
DateString = date_str.concat(time_str);
else
DateString = time_str;
}
else
DateString = date_str;
return DateString.trim();
}
private static String TimesString(double times) throws HpException
{
char str[] = new char[256];
int hour;
int min ;
int sec ;
int i = 0;
double totaltime;
totaltime = (double)(times * 24 * 3600);
sec = (int)dbl2intval(totaltime % 60);
sec %= 60;
totaltime = (totaltime - sec) / 60;
min = (int)dbl2intval(totaltime % 60);
min %= 60;
totaltime = (totaltime - min) / 60;
hour =(int)dbl2intval(totaltime);
str[i++] = ' ';
if(hour % 12 == 0)
{
str[i++] = '1';
str[i++] = '2';
}
else
i = Date2Str(hour % 12,i,str);
str[i++] = ':';
if(min < 10 && min != 0)
str[i++] = '0';
i= Date2Str(min,i,str);
str[i++] = ':';
if(sec < 10 && sec != 0)
str[i++] = '0';
i= Date2Str(sec,i,str);
str[i++] = ' ';
if(hour >= 12)
{
str[i++] = 'P';
str[i++] = 'M';
}
else
{
str[i++] = 'A';
str[i++] = 'M';
}
return (new String(str,0,i));
}
/**
* Returns a int representation of a double
**/
public static int dbl2intval(double d) throws HpException
{
int i = 0;
boolean flag = false;
double dec = 0;
if(d >= 2147483647.5 || d < - 2147483648.5)
throw new HpException(6,"Overflow");
i = (int)d;
dec = (double)((d - (double)((int)(d))) * 10);
if(dec == 0)
return i;
if(dec < 0)
dec = -dec;
if(dec < 5)
return i;
if(dec > 5)
{
if(d > 0)
i ++;
else
i --;
}
else
if(dec == 5)
{
if((i % 2) != 0) {
if(d > 0)
i ++;
else
i --;
}
}
return i;
}
private static String DaysString(int day){
int mouth1[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int mouth2[] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
int mouth3[];
char str[];
int i = 0;
int year = 0;
int mouth = 0;
day --;
if(day > 0)
{
year = 1900;
while(day>365)
{
day -= 365;
if(is_specyear(year))
day --;
year ++;
}
if(day > 0)
{
if(is_specyear(year))
mouth3 = mouth2;
else
mouth3 = mouth1;
for(mouth = 0; mouth < 13 ; mouth ++)
{
day -= mouth3[mouth];
if(day <= 0)
{
day += mouth3[mouth];
break;
}
}
}
else
if(day == 0)
{
year --;
mouth = 12;
day = 31;
}
}
else
if(day <= 0)
{
year = 1899;
while(day <-365)
{
day += 365;
if(is_specyear(year))
day++;
year --;
}
day = 365 + day;
if(is_specyear(year))
day++;
if(day > 0)
{
if(is_specyear(year))
mouth3 = mouth2;
else
mouth3 = mouth1;
for(mouth = 0; mouth < 13 ; mouth ++)
{
day -= mouth3[mouth];
if(day <= 0)
{
day += mouth3[mouth];
break;
}
}
}
else
if(day == 0)
{
year --;
mouth = 12;
day = 31;
}
}
str = new char[256];
if(mouth != 0)
i = Date2Str(mouth,i,str);
else
str[i++] = '1';
str[i++] = '/';
i = Date2Str(day,i,str);
str[i++] = '/';
if(year > 1929 && year < 2000)
year -= 1900;
i = Date2Str(year,i,str);
return (new String(str,0,i));
}
private static int Date2Str(int time,int i, char str[])
{
char s[] = new char[5];
int j = 0;
if(time == 0)
{
str[i++] = '0';
str[i++] = '0';
}
while(time > 0)
{
int in = (time % 10);
switch(in)
{
case 0: s[j++] = '0';break;
case 1: s[j++] = '1';break;
case 2: s[j++] = '2';break;
case 3: s[j++] = '3';break;
case 4: s[j++] = '4';break;
case 5: s[j++] = '5';break;
case 6: s[j++] = '6';break;
case 7: s[j++] = '7';break;
case 8: s[j++] = '8';break;
case 9: s[j++] = '9';break;
}
time = (int)(time / 10);
}
for(int m = j - 1; m >= 0 ; m--)
{
str[i++] = s[m];
}
return i;
}
private static boolean is_specyear(int year)
{
if((year % 4 == 0 && year % 100 != 0)
|| (year % 400 == 0))
return true;
return false;
}
/**
* Returns a currency representation of a double
**/
public static double dbl2currval(double d){
double dbl = ((double)(long)d);
boolean flag = false;
double dec = d - dbl;
if(dec == 0)
return d;
dbl = dbl + (double)(((int)(dec * 10000)) * 0.0001);
dec = (double)(dec * 10000);
double inc = dec - (double)((int)(dec));
if(inc == 0)
return d;
int lastnum = ((int)(dec)) % 10;
if(inc > 0.5)
flag = true;
else
if(inc == 0.5)
{
if(lastnum == 1 || lastnum == 2 || lastnum == 3 ||
lastnum == 6 || lastnum == 7)
flag = true;
}
if(flag == true)
{
if(d > 0)
dbl += 0.0001;
else
dbl -= 0.0001;
}
return dbl;
}
public static HCurr dbl2curr(double dbl)
{
double db=dbl2currval(dbl);
return new HCurr(db);
}
/**
*Convert byte to currency
*/
public static HCurr byte2curr(short val){
return new HCurr((new Short(val)).doubleValue());
}
/**
*Convert int to Currency
*/
public static HCurr int2curr(short val){
return new HCurr((new Short(val)).doubleValue());
}
/**
* Convert long to Currency
*/
public static HCurr lng2curr(int val){
return new HCurr((new Integer(val)).doubleValue());
}
/**
*Convert single to Currency
*/
public static HCurr sgl2curr(float val){
double dbl=(new Float(val)).doubleValue();
double curr=dbl2currval(dbl);
return new HCurr(curr);
}
/**
*Convert boolean to Currency
*/
public static HCurr bool2curr(boolean val){
double curr_dbl;
if(val)
curr_dbl=-1;
else
curr_dbl=0;
return new HCurr(curr_dbl);
}
public static short lng2int(int val) throws HpException
{
return (short)val;
}
public static short dbl2int(double val) throws HpException
{
int di=dbl2intval(val);
return (short)di;
}
public static int dbl2lng(double d) throws HpException
{
int dl = dbl2intval(d);
return dl;
}
public static float dbl2sgl(double d) throws HpException
{
return (float)(dbl2intval(d));
}
public static float lng2sgl(int val)
{
return (float)val;
}
/**
*Convert double to Currency
*/
public static HCurr dbl2cur(double val){
return new HCurr(val);
}
/**
* Convert String to Currency
*/
public static HCurr str2curr(String val) throws HpException
{
double d = str2dbl(val);
double curr=dbl2currval(d);
return new HCurr(curr);
}
/**
*Convert currency to boolean
*/
public static boolean curr2bool(HCurr curr){
if(curr.dblValue() !=0.0)
return true;
else
return false;
}
public static int curr2lng(HCurr curr) throws HpException {
int result=dbl2intval(curr.dblValue());
return result;
}
public static int sgl2lng(float f) throws HpException
{
return (new Float(f)).intValue();
}
public static short curr2int(HCurr curr) throws HpException{
short result=dbl2int(curr.dblValue());
return result;
}
/**
* Convert currency to single
*/
public static float curr2sgl(HCurr curr){
return curr.sglValue();
}
/**
* Convert currency to double
*/
public static double curr2dbl(HCurr curr){
return curr.dblValue();
}
/**
* Convert currency to string
*/
public static String curr2str(HCurr curr){
return curr.toString();
}
/**
* Convert currency to date
*/
public static HDate byte2date(short val){
return new HDate((new Short(val)).doubleValue());
}
/**
Convert string to date
*/
public static HDate str2date(String date) throws HpException
{
String s = date;
double dbl = 0;
try{
dbl = (new Double(s)).doubleValue( );
if( s.indexOf('.')!=-1 && dbl>0 )
{
String tmp = s.substring(s.indexOf('.')+1 );
double tmpd = (new Double(tmp)).doubleValue();
double tmpy = (new Double(s.substring(0,s.indexOf('.'))).doubleValue() );
if(tmpy<24 && tmpd<60 )
s = s.replace('.', ':');
}
}catch( Exception e){;}
try
{
dbl = hpstring.strtodbl(s);
}catch( HpException err)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -