Common JavaScript code (8)

Source: Internet
Author: User
Tags modulus

 

// 12

Extended functions for several JavaScript objects
Function checkbrowser ()
{
This. Ver = navigator. appversion
This. DOM = Document. getelementbyid? 1:0
This. IE6 = (this. Ver. indexof ("MSIE 6")>-1 & this. DOM )? 1:0;
This. ie5 = (this. Ver. indexof ("MSIE 5")>-1 & this. DOM )? 1:0;
This. ie4 = (document. All &&! This. DOM )? 1:0;
This. NS5 = (this. DOM & parseint (this. Ver)> = 5 )? 1:0;
This. ns4 = (document. Layers &&! This. DOM )? 1:0;
This. Mac = (this. Ver. indexof ('mac')>-1 )? 1:0;
This. OPE = (navigator. useragent. indexof ('Opera ')>-1 );
This. Ie = (this. IE6 | this. ie5 | this. ie4)
This. NS = (this. ns4 | this. NS5)
This. BW = (this. IE6 | this. ie5 | this. ie4 | this. NS5 | this. ns4 | this. Mac | this. OPE)
This. NBW = (! This. bw)
Return this;
}
/*
**************************************** **
Extended date functions
**************************************** **
*/

/*
========================================================== ===
// Convert to the upper-right Writing Date (Chinese)
========================================================== ===
*/
Date. Prototype. tocase = function ()
{
VaR digits = new array ('0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9', '10', '11', '12 ');
VaR unit = new array ('Year', 'month', 'day', 'point', 'Min', 'second ');
VaR year = This. getyear () + "";
VaR index;
VaR output = "";
//// // Get the year
For (Index = 0; index <year. length; index ++)
{
Output + = digits [parseint (Year. substr (index, 1)];
}
Output + = Unit [0];
/// // Get the month
Output + = digits [This. getmonth ()] + Unit [1];
/// // Get the day
Switch (parseint (this. getdate ()/10 ))
{
Case 0:
Output + = digits [This. getdate () % 10];
Break;
Case 1:
Output + = digits [10] + (this. getdate () % 10)> 0? Digits [(this. getdate () % 10)]: "");
Break;
Case 2:
Case 3:
Output + = digits [parseint (this. getdate ()/10)] + digits [10] + (this. getdate () % 10)> 0? Digits [(this. getdate () % 10)]: "");
Default:
Break;
}
Output + = Unit [2];
//// // Get it
Switch (parseint (this. gethours ()/10 ))
{
Case 0:
Output + = digits [This. gethours () % 10];
Break;
Case 1:
Output + = digits [10] + (this. gethours () % 10)> 0? Digits [(this. gethours () % 10)]: "");
Break;
Case 2:
Output + = digits [parseint (this. gethours ()/10)] + digits [10] + (this. gethours () % 10)> 0? Digits [(this. gethours () % 10)]: "");
Break;
}
Output + = Unit [3];
If (this. getminutes () = 0 & this. getseconds () = 0)
{
Output + = "whole ";
Return output;
}
//// // Score
Switch (parseint (this. getminutes ()/10 ))
{
Case 0:
Output + = digits [This. getminutes () % 10];
Break;
Case 1:
Output + = digits [10] + (this. getminutes () % 10)> 0? Digits [(this. getminutes () % 10)]: "");
Break;
Case 2:
Case 3:
Case 4:
Case 5:
Output + = digits [parseint (this. getminutes ()/10)] + digits [10] + (this. getminutes () % 10)> 0? Digits [(this. getminutes () % 10)]: "");
Break;
}
Output + = Unit [4];
If (this. getseconds () = 0)
{
Output + = "whole ";
Return output;
}
/// // Obtain the second
Switch (parseint (this. getseconds ()/10 ))
{
Case 0:
Output + = digits [This. getseconds () % 10];
Break;
Case 1:
Output + = digits [10] + (this. getseconds () % 10)> 0? Digits [(this. getseconds () % 10)]: "");
Break;
Case 2:
Case 3:
Case 4:
Case 5:
Output + = digits [parseint (this. getseconds ()/10)] + digits [10] + (this. getseconds () % 10)> 0? Digits [(this. getseconds () % 10)]: "");
Break;
}
Output + = Unit [5];

Return output;
}

/*
========================================================== ===
// Convert to Lunar Calendar
========================================================== ===
*/
Date. Prototype. tochinese = function ()
{
// Missing
}
/*
========================================================== ===
// Whether it is a leap year
========================================================== ===
*/
Date. Prototype. isleapyear = function ()
{
Return (0 = This. getyear () % 4 & (this. getyear () % 100! = 0) | (this. getyear () % 400 = 0 )));
}
/*
========================================================== ===
// Obtain the number of days of the month
========================================================== ===
*/
Date. Prototype. getdaycountinmonth = function ()
{
VaR MON = new array (12 );
Mon [0] = 31; Mon [1] = 28; Mon [2] = 31; Mon [3] = 30; Mon [4] = 31; mon [5] = 30;
Mon [6] = 31; Mon [7] = 31; Mon [8] = 30; Mon [9] = 31; Mon [10] = 30; mon [11] = 31;
If (0 = This. getyear () % 4 & (this. getyear () % 100! = 0) | (this. getyear () % 400 = 0) & this. getmonth () = 2)
{
Return 29;
}
Else
{
Return mon [This. getmonth ()];
}
}

/*
========================================================== ===
// Date comparison
========================================================== ===
*/
Date. Prototype. Compare = function (objdate)
{
If (typeof (objdate )! = "Object" & objdate. constructor! = Date)
{
Return-2;
}
VaR d = This. gettime ()-objdate. gettime ();
If (D> 0)
{
Return 1;
}
Else if (D = 0)
{
Return 0;
}
Else
{
Return-1;
}
}
/*
========================================================== ===
// Format the date
========================================================== ===
*/
Date. Prototype. format = function (formatstr)
{
VaR STR = formatstr;
STR = Str. Replace (/YYYY | yyyy/, this. getfullyear ());
STR = Str. Replace (/yy | YY/, (this. getyear () % 100)> 9? (This. getyear () % 100). tostring (): "0" + (this. getyear () % 100 ));
STR = Str. Replace (/MM/, this. getmonth ()> 9? This. getmonth (). tostring (): "0" + this. getmonth ());
STR = Str. Replace (/M/G, this. getmonth ());
STR = Str. Replace (/DD | DD/, this. getdate ()> 9? This. getdate (). tostring (): "0" + this. getdate ());
STR = Str. Replace (/d | D/g, this. getdate ());
STR = Str. Replace (/HH | hh/, this. gethours ()> 9? This. gethours (). tostring (): "0" + this. gethours ());
STR = Str. Replace (/h | h/g, this. gethours ());
STR = Str. Replace (/MM/, this. getminutes ()> 9? This. getminutes (). tostring (): "0" + this. getminutes ());
STR = Str. Replace (/M/G, this. getminutes ());
STR = Str. Replace (/SS | SS/, this. getseconds ()> 9? This. getseconds (). tostring (): "0" + this. getseconds ());
STR = Str. Replace (/S | S/g, this. getseconds ());
Return STR;
}

/*
========================================================== ===
// Date object of the instance directly by string
========================================================== ===
*/
Date. Prototype. instancefromstring = function (STR)
{
Return new date ("2004-10-10". Replace (/-/g ,"//"));
}
/*
========================================================== ===
// Obtain the date following the date, year, month, and date plus a number
========================================================== ===
*/
Date. Prototype. dateadd = function (interval, number)
{
VaR date = this;
Switch (interval)
{
Case "Y ":
Date. setfullyear (date. getfullyear () + number );
Return date;
Case "Q ":
Date. setmonth (date. getmonth () + number * 3 );
Return date;
Case "M ":
Date. setmonth (date. getmonth () + number );
Return date;
Case "W ":
Date. setdate (date. getdate () + number * 7 );
Return date;

Case "D ":
Date. setdate (date. getdate () + number );
Return date;
Case "H ":
Date. sethours (date. gethours () + number );
Return date;
Case "M ":
Date. setminutes (date. getminutes () + number );
Return date;
Case "S ":
Date. setseconds (date. getseconds () + number );
Return date;
Default:
Date. setdate (D. getdate () + number );
Return date;
}
}
/*
========================================================== ===
// Calculate the date, year, month, and day of the two dates
========================================================== ===
*/
Date. Prototype. datediff = function (interval, objdate)
{
// Missing
}

/*
**************************************** **
Number function expansion
**************************************** **
*/
/*
========================================================== ===
// Convert to an uppercase Chinese number
========================================================== ===
*/
Number. Prototype. tochinese = function ()
{
VaR num = this;
If (! /^/D * (/./D *)? $/. Test (Num) {alert ("number is wrong! "); Return" number is wrong! ";}
VaR AA = new array ("zero", "one", "two", "three", "Si", "Wu", "Lu", "Lu ", "success", "fail ");
VaR BB = new array ("", "Pick", "success", "point ","");

VaR A = ("" + num ). replace (/(^ 0 *)/g ,""). split (". "), k = 0, Re = "";
For (VAR I = A [0]. Length-1; I> = 0; I --)
{
Switch (k)
{
Case 0: Re = BB [7] + RE; break;
Case 4: If (! New Regexp ("0 {4} // d {" + (A [0]. length-i-1) + "} $"). Test (A [0])
Re = BB [4] + RE; break;
& Nb, SP; case 8: Re = BB [5] + RE; bb [7] = BB [5]; k = 0; break;
}
If (K % 4 = 2 & A [0]. charat (I + 2 )! = 0 & A [0]. charat (I + 1) = 0) Re = AA [0] + RE;
If (A [0]. charat (I )! = 0) Re = AA [A [0]. charat (I)] + BB [K % 4] + RE; k ++;
}
If (A. length> 1) // Add the fractional part (if any)
{
Re + = BB [6];
For (VAR I = 0; I <A [1]. length; I ++) RE + = AA [A [1]. charat (I)];
}
Return re;
}
/*
========================================================== ===
// Retain the number of decimal places
========================================================== ===
*/
Number. Prototype. tofixed = function (LEN)
{
If (isnan (LEN) | Len = NULL)
{
Len = 0;
}
Else
{
If (LEN <0)
{
Len = 0;
}
}
Return math. Round (this * Math. Pow (10, Len)/Math. Pow (10, Len );
}
/*
========================================================== ===
// Convert to a large write amount
========================================================== ===
*/
Number. Prototype. tomoney = function ()
{
// Constants:
VaR maximum_number = 99999999999.99;
// Predefine the radix characters and currency symbols for output:
VaR cn_zero = "zero ";
VaR cn_one = "1 ";
VaR cn_two = "II ";
VaR cn_three = "";
VaR cn_four = "Si ";
VaR cn_five = "Wu ";
VaR cn_six = "";
VaR cn_seven = "success ";
VaR cn_eight = "success ";
VaR cn_nine = "canonical ";
VaR cn_ten = "pick up ";
VaR cn_hundred = "success ";
VaR cn_thousand = "Hangzhou ";
VaR cn_ten_thousand = "Ten Thousand ";
VaR cn_hundred_million = "";
VaR cn_symbol = "";
VaR cn_dollar = "RMB ";
VaR cn_ten_cent = "";
VaR cn_cent = "points ";
VaR cn_integer = "whole ";

// Variables:
VaR integral; // represent integral part of digit number.
VaR decimal; // represent decimal part of digit number.
VaR outputcharacters; // The output result.
VaR parts;
VaR digits, radices, bigradices, Decimals;
VaR zerocount;
VaR I, P, D;
VaR quotient, modulus;

If (this> maximum_number)
{
Return "";
}

// Process the coversion from currency digits to characters:
// Separate integral and decimal parts before processing coversion:
Parts = (This + ""). Split (".");
If (parts. length> 1)
{
Integral = parts [0];
Decimal = parts [1];
// Cut down redundant decimal digits that are after the second.
Decimal = decimal. substr (0, 2 );
}
Else
{
Integral = parts [0];
Decimal = "";
}
// Prepare the characters corresponding to the digits:
Digits = new array (cn_zero, cn_one, cn_two, cn_three, cn_four, cn_five, cn_six, cn_seven, cn_eight, cn_nine );
Radices = new array ("", cn_ten, cn_hundred, cn_thousand );
Bigradices = new array ("", cn_ten_thousand, cn_hundred_million );
Decimals = new array (cn_ten_cent, cn_cent );
// Start processing:
Outputcharacters = "";
// Process integral part if it is larger than 0:
If (number (Integral)> 0)
{
Zerocount = 0;
For (I = 0; I <integral. length; I ++)
{
P = integral. Length-I-1;
D = integral. substr (I, 1 );
Quotient = P/4;
Modulus = P % 4;
If (D = "0 ")
{
Zerocount ++;
}
Else
{
If (zerocount> 0)
{
Outputcharacters + = digits [0];
}
Zerocount = 0;
Outputcharacters + = digits [number (d)] + radices [modulus];
}
If (modulus = 0 & zerocount <4)
{
Outputcharacters + = bigradices [quotient];
}
}
Outputcharacters + = cn_dollar;
}
// Process decimal part if there is:
If (decimal! = "")
{
For (I = 0; I <decimal. length; I ++)
{
D = decimal. substr (I, 1 );
If (D! = "0 ")
{
Outputcharacters + = digits [number (d)] + decimals [I];
}
}
}
// Confirm and return the final output string:
If (outputcharacters = "")
{
Outputcharacters = cn_zero + cn_dollar;
}
If (decimal = "")
{
Outputcharacters + = cn_integer;
}
Outputcharacters = cn_symbol + outputcharacters;
Return outputcharacters;
}

Number. Prototype. toimage = function ()
{
VaR num = array (
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0xf, 0x5, 0x5, 0x5, 0xf }",
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0x4, 0x4, 0x4, 0x4 }",
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0xf, 0x4, 0xf, 0x1, 0xf }",
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0xf, 0x4, 0xf, 0x4, 0xf }",
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0x5, 0x5, 0xf, 0x4, 0x4 }",
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0xf, 0x1, 0xf, 0x4, 0xf }",
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0xf, 0x1, 0xf, 0x5, 0xf }",
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0xf, 0x4, 0x4, 0x4, 0x4 }",
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0xf, 0x5, 0xf, 0x5, 0xf }",
"# Define t_width 3/N # define t_height 5/nstatic char t_bits [] = {0xf, 0x5, 0xf, 0x4, 0xf }"
);
VaR STR = This + "";
VaR iindex
VaR result = ""
For (iindex = 0; iindex <Str. length; iindex ++)
{
Result + = "
}
Return result;
}

/*
**************************************** **
Other function extensions
**************************************** **
*/

/*
========================================================== ===
// Verify the class function
========================================================== ===
*/
Function isempty (OBJ)
{
OBJ = Document. getelementsbyname (OBJ). Item (0 );
If (TRIM (obj. Value) = "")
{

If (obj. Disabled = false & obj. readonly = false)
{
OBJ. Focus ();
}
Return true;
}
Else
{
Return false;
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.