Formatting number

   input: Float
   Return type: String(on code) / text 
Code Coverage: Almost entirely.

Introduction: when encountering large numbers, the comma insertion is preferred. 
also when going above 9 quadrillion, the number starts to have zeros on the right even if the number keeps being multiplied by non-whole numbers until it has reaches 1e21 which is one sextillion. 

the function of this behaviour is to make large numbers (form 1,000 to 1e21) easier to see.
the first picture show that this behaviour places decimals to the numbers in this case between 1,000 and a quadrillion or 1e15, 

The second picture shows that this behaviour converts in this case 1e15 or larger into standard form. 




The code is described below. 
in this case, the input is __init.



and here is the full code:
public static function _customBlock_formatted_number(__init:Float):String
{
var a:Float = __init;
if(a < 0)
{
a = a * -1; // inverts negative to positive values
}
var b:Float = Math.floor(a); //discards decimals
var c:Float = a-b; //discard the whole number

var d:Float = 0; //intended for int
var e:Float = 0; //intended for int
var f:Float = 0; //auxiliary module
var g:Float = 0;

var r:String = "";
var s:String = "";
var t:String = "";
if(a == 0)
{
return "0";
}
if(a >= 1e21) // 1e21 or larger are converted to standard form by default.
{
return ("" + __init); 
}
if(a < 1000)
{
return ("" + __init);
}
if(a <1e15)
{
s = (""+ b); d = s.length-3; t=s;
while(d > 0)
{
t = t.substring(0,Std.int(d)) + "," + t.substring(Std.int(d),t.length);
d = d-3; //in this commas to be placed every 3 digits
}
if(c > 0) // in this case, decimals are included
{
r = ("." + c);
}

if(__init < 0)
{
 return "-" + t + r;
}
else 
{
 return "" + t + r;
}
 
}
else // converts numbers bigger than 1 quadrillion or 1e15 to standard form
{
d = Math.floor(Math.log(a) / Math.log(10)); // log base 10;
e = Math.pow(10,d);
f = a/e;
if(__init < 0)
{
return "-" + f + "e" + d;  
}
else
{
return "" + f + "e" + d;
}
}
return ("" + __init);
}



















No comments:

Post a Comment