CONST REG =/^ ([0-9]+) \. ([0-9]*) $/;//determine if it is a floating pointConst ISFLOAT =function(number) {returnreg.test (number);}//remove Decimal to IntegerConst FLOATTOINT =function(Head,tail) {//head:string tail:string //head and tail are both string number ("005") can go to 0Let result = head +Number (tail); returnNumber (result);//Handling Head 0 cases}//What is the length after the decimal point e-n n?Const Taillen =function(tail) {//tail:string returntail.length;}//match array contains data on both left and right of the decimal pointConst HANDLERFLOAT =function(float){//float:float returnReg.exec (float);}//non-floating-point numbers determine if the incoming data type is a reasonable dataConst Canhandler =function(data) {Let toString=Object.prototype.toString; Let IsNaN= data =typeofdata=== ' number ' && data!== +data; Let Isboolean= Data = = =true|| data = = =false|| Tostring.call (data) = = = "[Object Boolean]"; Let IsNull= Data = = =NULL; Let isundefined= Data = = =void0; Let Isnumber=typeofdata = = = ' Number ' | | Tostring.call (data) = = = "[Object number]"; Let Isstring=typeofdata = = = ' String ' | | Tostring.call (data) = = = "[Object String]"; if(IsNaN (data) | | isboolean | | isNull | |isundefined) { //throw new Error ("Only number or String type");Console.log ("Only number or String type"); return false; } returnIsnumber | | (isstring &&!)IsNaN (number (data)));}//APIConst floatmult=function(F1,F2) {if(Isfloat (F1) &&isfloat (F2)) { varEnum1,enum2,match1,match2,int1,int2; //Match ArrayMatch1 =handlerfloat (F1); MATCH2=handlerfloat (F2); //This is used to convert to scientific notation after the decimal point .ENUM1 = match1[2].length; ENum2= Match2[2].length; //remove Decimal to IntegerInt1 = Floattoint (match1[1],match1[2]); Int2= Floattoint (match2[1],match2[2]); //0.2*0.3 = 2*3* (e-2) = (2*3)/100 return(Int1*int2)/(Math.pow (Ten, (enum1+enum2))); }Else{ returnCanhandler (F1) && Canhandler (F2) && F1 *F2; }}//Exposed InterfaceModule.exports =Floatmult;console.log (Floatmult (0.2,0.3));//0.06Console.log (Floatmult (1.3,1.2));//1.56Console.log (Floatmult (123.4,6456.3));//796707.42Console.log (Floatmult (. 2,.3));//0.06Console.log (Floatmult (1,0.3));//0.3Console.log (Floatmult (12,55));//660Console.log (Floatmult ("0.2", "0.3"));//0.06Console.log (Floatmult ("0.2", 0.3));//0.06Console.log (Floatmult ("12", "25"));// -Console.log (Floatmult ("12", 55));//660Console.log (Floatmult ("1.3.3", 1.3));//falseConsole.log (Floatmult (true, 0.3));//false "only number or String type"Console.log (Floatmult (NULL, 0.2));//false "only number or String type"Console.log (Floatmult (void0,0.3));//false "only number or String type"
Self-write small function handles JavaScript 0.3*0.2 floating-point type multiplication problem