<?php

/*
   METRIC MASS AND WEIGHT EQUIVALENTS TABLE CALCULATOR
   REVISED VERSION
   Handles to 70 significant figures or decimals
   for high-precision theoretical computations.

   AUTHOR   : Jay Tanner - 2025
   LANGUAGE : PHP v8.2.12
   LICENSE  : Public Domain
*/

   
ob_start();



// Define the program cookie name and set it to expire in 30 days.
// This cookie can be shared by other programs in the same common
// home folder together.

   
$CookieName 'Metric_Mass_and_Weight_Equivalents_Table_Calculator';
   
$SetToExpireIn30Days time() + 30*86400;

// Define script path and filename.
   
$_AUTHOR_          'Jay Tanner of Waterloo, NY, USA';
   
$_PROGRAM_VERSION_ ''$at "&#97;&#116;"$UTC "&#85;&#84;&#67;";
   
$_SCRIPT_PATH_     Filter_Input(INPUT_SERVER'SCRIPT_FILENAME');
   
$_RUN_             Filter_Input(INPUT_POST'SCRIPT_NAME');

// Define internal document page title, HTML page heading text and revision date
   
$_INTERNAL_TITLE_  "Metric Mass and Weight Equivalents Table Calculator";
   
$_INTERFACE_TITLE_ "<span style='font-size:15pt;'>Metric Mass and Weight Equivalents Table Calculator</span><br><span style='font-size:10pt;'><br>by $_AUTHOR_</span>";
   
$_REVISION_DATE_   $_PROGRAM_VERSION_ .'Revised: 'gmdate("l, F d, Y $at h:i:s A   $UTC"FileMTime($_SCRIPT_PATH_));

// Define main TextArea text, background colors
// and HTML row span.
   
$TxColor 'black';
   
$BgColor 'white';
   
$ColSpan 1// Same as number of input variables.

   
$BAR Str_Repeat('='78);

// Define units table.
   
$UnitsTableText =
"There are 18 mass/weight units symbols recognized by this program as listed
in the table below.  The symbols are NOT case sensitive.  Many other units
exist, but these are among the most commonly used.

MASS AND WEIGHT UNITS SYMBOLS TABLE
==============================================================================
SYMBOL  REPRESENTED MASS/WEIGHT UNIT
------  ---------------------------------------------------------------------
  pg    picogram
  ng    nanogram
  ug    microgram - The letter 'u' is substituted for Greek letter '&mu;' (mu).
  mg    milligram
  gr    grain
  pwt   pennyweight
  c     carat
  g     gram
  dr    dram  (avdp.)
  oz    ounce (avdp.)
 troz   Troy Ounce
  N     Newton
  lb    pound (avdp.)
 trlb   Troy pound
  kg    kilogram
  st    stone
  ton   ton (2000 lb avdp.)
  t     metric ton (1000 kg)

$BAR
NOTES

[1]
Mass and weight are NOT the same thing but can be measured in the same
units, which can sometimes lead to confusion between them. When you put
an object on a scale, you are measuring its weight, not its mass.

[2]
Mass is the measure of the physical substance contained in something while
the weight is the downward force or pressure imparted by local gravitation
to the mass.   An object's weight may change with location even though its
mass remains constant.

[3]
All computed conversions are based on the EXACTLY defined standard SI units.

[4]
The extended decimal precision is for theoretical computations and decimals
are truncated (not rounded off) at a maximum limit of 68 decimals.

$BAR";







// Do this only if [COMPUTE] button was clicked.
   
$w Filter_Input(INPUT_POST'ComputeButton');

   if (!IsSet(
$w))

// ----------------------------------------------------
// If [COMPUTE] button was clicked and an active cookie
// exists, then restore the previous interface settings
// from it.
     
{
      
$w Filter_Input(INPUT_COOKIE$CookieName);

   if (IsSet(
$w))
      {
       
$CookieDataString trim(Filter_Input(INPUT_COOKIE$CookieName));

       list (
$var1) = preg_split("[\|]"$CookieDataString);

   
$var1 Split_Number_and_Symbol ($var1);

   @list(
$L$U) = Preg_Split("[ ]"$var1);

   if(
$U == '') {$U 'kg';}
      } 
// End of  IsSet(...)

   
else

// Set the initial default interface startup values.

 
{
  
$var1 '1.0 kg';

  
$L '1.0';
  
$U 'kg';

// Store interface settings in cookie.
   
$CookieDataString "$var1";

   
setcookie ($CookieName$CookieDataString$SetToExpireIn30Days);
  }
      } 
// End of  if (!isset(_POST['ComputeButton']))







// ======================================
// Read values of all interface settings.
// Empty values are set to defaults.

   
$w Filter_Input(INPUT_POST'ComputeButton');

   if (isset(
$w))
{
   
$var1 trim(Filter_Input(INPUT_POST'var1'));

   if (
$var1 == '') {$var1 '0.0 kg';}

   
$var1 Split_Number_and_Symbol ($var1);

   @list(
$L$U) = Preg_Split("[ ]"$var1);
   if (
$U == '') {$U 'kg';}
   if (
$L == '') {$L '0.0';}

// -----------------------------------
// Store interface settings in cookie.

   
$CookieDataString "$var1";

   
SetCookie ($CookieName$CookieDataString$SetToExpireIn30Days);
}





// ========================================
// Check for error. FALSE = Error detected.
// Change this code to detect errors.
//
// Initially, any non-number causes an error

   
$ErrFlag TRUE;
   
$ErrMssg '';

   
$var1 PReg_Replace("/\s+/"" "trim($var1));

   if
  (
   
Is_Numeric($var1)
   or 
Substr_Count($var1' ') > 1
   
or @Check_Mass_Units($U) === FALSE
   
or Substr_Count($var1'.') > 1
  
)
  {
   
$ErrFlag FALSE;

   if (
Check_Mass_Units($U) === FALSE)
   {
   
$ErrMssg =
"Invalid units symbol:
'
$U'

The mass/weight units symbol should be one of the units symbols given
in the table below.  The symbols are NOT case sensitive."
;}

else
{
   
$ErrMssg .=
"Invalid mass or weight value or missing units symbol:
'
$L'

The mass or weight must be entered as a plain number followed by one of the
mass/weight units symbols given in the table below."
;
}

  }

// exit("$w");




// TextArea2.
   
$TextArea2Text $UnitsTableText;



// ================================
// Check if error (FALSE) detected.

if ($ErrFlag === FALSE)
 {
  
$TxColor 'white';
  
$BgColor '#CC0000';

  
$TextArea1Text =
"=== ERROR ====================================================================

$ErrMssg";
 }

else

// ====================================================
// Begin computations here if no errors detected above.

  
{
   
$L    RTrim(RTrim(bcAdd('0'$L68), '0'), '.');
   
$var1 "$L $U";
   
$U Check_Mass_Units ($U);

   
$pg   bc_Mass_to_Mass ("$L $U",   'pg');  $_pg   bc_Big_Int_Name($pg,     'pg');
   
$ng   bc_Mass_to_Mass ("$L $U",   'ng');  $_ng   bc_Big_Int_Name($ng,     'ng');
   
$ug   bc_Mass_to_Mass ("$L $U",   'ug');  $_ug   bc_Big_Int_Name($ug,     'ug');
   
$mg   bc_Mass_to_Mass ("$L $U",   'mg');  $_mg   bc_Big_Int_Name($mg,     'mg');
   
$gr   bc_Mass_to_Mass ("$L $U",   'gr');  $_gr   bc_Big_Int_Name($gr,     'gr');
   
$pwt  bc_Mass_to_Mass ("$L $U",   'pwt'); $_pwt  bc_Big_Int_Name($pwt,   'pwt');
   
$c    bc_Mass_to_Mass ("$L $U",    'c');  $_c    bc_Big_Int_Name($c,       'c');
   
$g    bc_Mass_to_Mass ("$L $U",    'g');  $_g    bc_Big_Int_Name($g,       'g');
   
$dr   bc_Mass_to_Mass ("$L $U",   'dr');  $_dr   bc_Big_Int_Name($dr,     'dr');
   
$oz   bc_Mass_to_Mass ("$L $U",   'oz');  $_oz   bc_Big_Int_Name($oz,     'oz');
   
$troz bc_Mass_to_Mass ("$L $U"'troz');  $_troz bc_Big_Int_Name($troz'troz');
   
$N    bc_Mass_to_Mass ("$L $U",    'N');  $_N    bc_Big_Int_Name($N,       'N');
   
$lb   bc_Mass_to_Mass ("$L $U",   'lb');  $_lb   bc_Big_Int_Name($lb,     'lb');
   
$trlb bc_Mass_to_Mass ("$L $U"'trlb');  $_trlb bc_Big_Int_Name($trlb'trlb');
   
$kg   bc_Mass_to_Mass ("$L $U",   'kg');  $_kg   bc_Big_Int_Name($kg,     'kg');
   
$st   bc_Mass_to_Mass ("$L $U",   'st');  $_st   bc_Big_Int_Name($st,     'st');
   
$ton  bc_Mass_to_Mass ("$L $U",  'ton');  $_ton  bc_Big_Int_Name($ton,   'ton');
   
$t    bc_Mass_to_Mass ("$L $U",    't');  $_t    bc_Big_Int_Name($t,       't');


//   if ($U <> 'ton' and $U <> 'tons' and $U <> 't' and $U <> 'ts')

   
$_s1 $_s2 '';

   
$var1 "$L $U";

   if(
$U == 'ton' or $U == 'tons')
  {
   
$_s1 = (FloatVal($ton) <> 1)? 's':'';
   if (
FloatVal($ton) < and FloatVal($ton) <> 0){$_s1 '';}
      
$var1 "$L $U$_s1";
  }

   
$_L bc_Big_Int_Name($L$U);


   
$TextArea1Text =
"Mass or Weight Equivalents to:
$L $U$_L
$BAR

$pg pg$_pg
$ng ng$_ng
$ug ug$_ug
$mg mg$_mg
$gr gr$_gr
$c c$_c
$g g$_g
$pwt pwt$_pwt
$dr dr$_dr
$oz oz$_oz
$troz troz$_troz
$N N$_N
$trlb trlb$_trlb
$lb lb$_lb
$kg kg$_kg
$st st$_st
$ton ton$_s1
$t t$_t";

// End of  else{...}








// Determine the number of text rows to use in the output TextAreas.
   
$TextArea1Rows substr_count($TextArea1Text"\n");
   
$TextArea2Rows substr_count($TextArea2Text"\n");



print <<< _HTML

<!DOCTYPE HTML>

<head>

<title>Quick Metric Weight/Mass Equivalents Table Calculator</title>

<style type='text/css'>
BODY
{color:silver; background-color:black;  font-family:Verdana; font-size:15px}

TABLE
{font-size:12px; border: 1px solid black;}

TD
{color:black; background-color:white;  line-height:125%; font-size:12px; padding:6px; text-align:center;}

DIV
{color:black; background-color:white;  font-family:Verdana; text-align:justify; padding:4px; font-size:16px;}

PRE
{color:black; background-color:white;  font-family:monospace; font-size:14px; font-weight:bold; text-align:left; padding:4px;}

CODE
{font-family:monospace; font-size:15px; font-weight:normal;}

TEXTAREA
{color:black; background-color:white; font-family:monospace; font-size:13pt; font-weight:bold; box-shadow:2px 3px 4px #888888; border-radius: 8px; border:1px solid black; padding:4px; white-space:pre; line-height:135%;}

INPUT[type="text"]::-ms-clear {width:0; height:0;}

INPUT[type="text"]
{color:black; background-color:white; font-size:14pt; font-family:monospace; font-weight:bold; text-align:center; box-shadow:2px 2px 3px #666666; border:2px solid black;}

INPUT[type="text"]:focus
{background-color:white; box-shadow:2px 2px 3px #666666; font-family:monospace; font-size:14pt; border:2px solid red; text-align:center; font-weight:bold;}

INPUT[type="submit"]
{font-weight:bold;}

INPUT[type="submit"]:hover
{box-shadow:2px 2px 5px #555555; font-weight:bold;}

INPUT[type="checkbox"]:checked
{box-shadow:2px 2px 3px #555555;}

HR {background-color:black; height:2px; border:0px;}

A:link,  A:visited {background-color:transparent; color:DarkCyan; font-family:Verdana; font-size:12px; font-weight:bold; text-decoration:none; line-height:150%; padding:2px;}
A:hover, A:active  {background-color:transparent; color:red;  font-family:Verdana; font-size:12px; font-weight:bold; text-decoration:none; line-height:150%; padding:2px;}

::selection      {background-color:yellow; color:black;}  /* Only affects IE9+ and Safari     */
::-moz-selection {background-color:yellow; color:black;}  /* Only affects FireFox and Mozilla */
</style>

</head>

<body>

<form name='form1' method='post' action="
$_RUN_">

<table align='center' width='800' border='0' cellspacing='1' cellpadding='3'>

<tr><td colspan="
$ColSpan" style='color:white; background-color:#000066; border:2px solid white; border-radius:8px 8px 0px 0px;'>$_INTERFACE_TITLE_</td></tr>


<tr>
<td style='line-height:150%;'><br><span>Enter Mass or Weight Value Followed by Any One of the Recognized Units Symbols</span><br>
<input name='var1'  type='text' value="
$var1"  size='76' maxlength='75' title=' pg  ng  ug  mg  gr  pwt  c  g  dr  oz  troz  lb  trlb  kg  st  N  ton  t '>
</td>



</tr>




<tr><td colspan="
$ColSpan" style='background-color:black;'>
<input name="ComputeButton" type="submit" value=" C O M P U T E ">
</td></tr>

<!-- Yellow source code view link. --->
<tr>
<td colspan='1' style='background:transparent; color:black; font-size:10pt;
                       text-align:center;'>
<a href='View-Source-Code.php' target='_blank'
   style='font-family:Verdana; color:black; background:yellow;
         text-decoration:none; border:1px solid black; padding:4px;
         border-radius:4px;'>
         &nbsp;<span style='font-weight:normal;'>View/Copy PHP Source Code</span>&nbsp;</a>
</td>
</tr>


<tr>
<td colspan="
$ColSpan" style='background-color:black; color:silver;'>Double-Click Within Table Area to Select ALL Text<br>
<textarea style='font-size:18px; color:
$TxColor; background-color:$BgColor; padding:6px; box-shadow:none; border:2px solid white;' name="TextArea1Text" cols="81" rows="$TextArea1Rows" readonly OnDblClick="this.select();">
$TextArea1Text
</textarea></td>
</tr>



<tr><td colspan="
$ColSpan" style='background-color:black;'>
<textarea style='font-size:18px; color:black; background-color:LightYellow; padding:6px; box-shadow:none;' name="TextArea2Text" cols="81" rows="
$TextArea2Rows" readonly>
$TextArea2Text
</textarea>
</td></tr>


<tr><td colspan="
$ColSpan" style='color:gray; background-color:black;'>Program by $_AUTHOR_<br>$_REVISION_DATE_</td></tr>

</table>
</form>

</body>


_HTML;





   function 
Split_Number_and_Symbol ($NumAlphaStr)
{
   
$ErrorFlag FALSE;

   
$wStr trim($NumAlphaStr);

   if (
Is_Numeric($wStr)) {return trim($wStr);}

   
$wStr PReg_Replace("/\s+/"" "trim($wStr));

   if(
Substr_Count($wStr' ') > 1) {return trim($wStr);}

   
$wLen   StrLen($wStr);
   
$nStr   $sStr '';
   
$dCount 0;
   
$dErrorFlag FALSE;

// Search for first non-numeric character within argument string.
// Everything from that character onward is the symbol part.
// Ignore decimal characters for now.
// NOT for signed numbers.

   
for ($i=0;   $i $wLen;   $i++)
  {
   
$CurrChar Substr($wStr$i1);

// Check if digit or decimal.
   
if (Is_Numeric($CurrChar) or $CurrChar == '.')
      {
       
$nStr .= $CurrChar;
      }


  } 
// End of  for (...)

// Check for extra decimals.  Only one allowed.
   
if(Substr_Count($nStr'.') > 1) {return trim($wStr);}
   if(
Substr_Count($nStr' ') > 1) {return trim($wStr);}

// Get index to symbol part.
   
$j = @StrPos($wStr$nStr strlen($nStr));

   
$sStr Str_Replace($nStr''$wStr);

return 
Str_Replace('  '' '"$nStr $sStr");
}





/*
 There are 17 length units symbols recognized by this program.

 They are:

 SYMBOL    UNITS

  mcg      micrograms

*/

   
function Check_Mass_Units ($UnitsSymbol)
{
// Define length units symbols table.
   
$SymbolsTable ' pg ng ug mg gr pwt c g dr oz troz lb trlb kg st n ton tons t ';

// Read units symbol argument, force it to lower
// case and then embed it between single spaces.
   
$U ' '.StrToLower(trim($UnitsSymbol)).' ';

   if (
$U == ' tons ' or $U == ' tonss ') {$U ' ton ';}

// Check to see if the units symbol is found within the symbols table.
// If not found, then FALSE is returned.  Otherwise, the symbol itself
// is returned.
   
if (StrPos($SymbolsTable$U) === FALSE)
      {return 
FALSE;}
   else
{
   
$U trim($U); // Remove padding spaces around the units symbol.

// Convert certain specified symbols to uppercase, if indicated.
   
if
  (
      
$U == 'n'
  
)
  {   
$U StrToUpper($U);}

// End of  else {...}

   
return $U;

// End of  Check_Mass_Units (...)





/*
==============================================================================
 Length to Length conversion for arbitrary precision arithmetic.

 This function interconverts between any of eighteen length or distance
 units.  The length/distance units symbols are NOT case sensitive.

 This is a 110-decimal arbitrary precision program.

 ----------
 ARGUMENTS:
 $LengthAndUnits = Numerical value and units symbol of length to convert.

                   IMPORTANT NOTE: There MUST be at least one space between
                   the number value and the units symbol in the string.

 $ToUnitsStr     = The units symbol corresponding to the computed output.

 -------
 ERRORS:
 On error FALSE is returned.
 An error occurs if the length value is non-numeric or either
 of the input or output length units symbols are unrecognized.

LENGTH UNITS SYMBOLS TABLE
==============================================================================
 SYMBOL    REPRESENTED LENGTH UNIT
--------  --------------------------------------------------------------------
  mcg      microgram
  mg       milligram

==============================================================================
*/

   
function bc_Mass_to_Mass ($MassAndUnits$ToUnitsStr$Decimals=70)
{
// Define conversion constants for internal use only.
// These constants could also be defined externally
// for global use by any programs that need them.

// Set internal arbitrary precision working decimals.
   
$Q 110;

// Set arbitrary precision decimals limit.
// From 0 to 110 decimals limit. Default = 70
   
$q abs(IntVal(trim($Decimals)));

   if (
$q $Q) {$q $Q;}

// ----------------------------------------------
// This function was designed to be transportable
// and fully self-contained, constants and all.

   
$KG_PER_PG  '0.000000000000001';
   
$KG_PER_NG  '0.000000000001';
   
$KG_PER_UG  '0.000000001';
   
$KG_PER_MG  '0.000001';
   
$KG_PER_GR  '0.00006479891';
   
$KG_PER_C   '0.0002';
   
$KG_PER_G   '0.001';
$KG_PER_PWT  '0.00155517384'// *

$KG_PER_SLUG '';

   
$KG_PER_DR  '0.0017718451953125';
   
$KG_PER_OZ  '0.028349523125';
$KG_PER_TR_OZ '0.0311034768'// *
$KG_PER_TR_LB '0.3732417216'// *
   
$KG_PER_LB  '0.45359237';
   
$KG_PER_KG  '1';
   
$KG_PER_ST  '6.35029318';
   
$KG_PER_N   '0.1019716212977928242570092743189570342573661749934993091422657074536156587621664890660929063441644190';
   
$KG_PER_TON '907.18474';
   
$KG_PER_TONNE   '1000';

/*
   Parse input value and separate the numerical value from the symbol.

   Read numeric value and then delete it from the string, leaving only
   the units symbol behind, if the number value is valid.

   NOTE: Scientific notation is NOT allowed, ONLY standard numbers.

*/

// Parse numerical value and space-delimited units symbol,
   
list ($v,$u) = PReg_Split("[ ]"PReg_Replace("/\s+/"' '$MassAndUnits));
   
$w "$v $u";
   list(
$x$FromUnits) = PReg_Split("[ ]"PReg_Replace("/\s+/"' '$w));

// -------------------------------------------------------------
// Get input units symbol. Units symbols are NOT case sensitive.

   
$FromUnitsSymbol StrToUpper(trim($FromUnits));

// ----------------------------------------
// Determine which conversion factor to use
// according to the units symbol argument.
   
$KG_PER_PG    '0.000000000000001';
   
$KG_PER_NG    '0.000000000001';
   
$KG_PER_UG    '0.000000001';
   
$KG_PER_MG    '0.000001';
   
$KG_PER_GR    '0.00006479891';
   
$KG_PER_C     '0.0002';
   
$KG_PER_G     '0.001';
   
$KG_PER_PWT   '0.00155517384';
   
$KG_PER_DR    '0.0017718451953125';
   
$KG_PER_OZ    '0.028349523125';
   
$KG_PER_TROZ  '0.0311034768';
   
$KG_PER_N     '0.1019716212977928242570092743189570342573661749934993091422657074536156587621664890660929063441644190';
   
$KG_PER_TRLB  '0.3732417216';
   
$KG_PER_LB    '0.45359237';
   
$KG_PER_KG    '1';
   
$KG_PER_ST    '6.35029318';
   
$KG_PER_TON   '907.18474';
   
$KG_PER_TONNE '1000';



   switch (
$FromUnitsSymbol)
  {
   case 
'PG'    $f $KG_PER_PG;    break;
   case 
'NG'    $f $KG_PER_NG;    break;
   case 
'UG'    $f $KG_PER_UG;    break;
   case 
'MG'    $f $KG_PER_MG;    break;
   case 
'GR'    $f $KG_PER_GR;    break;
   case 
'PWT'   $f $KG_PER_PWT;   break;
   case 
'C'     $f $KG_PER_C;     break;
   case 
'G'     $f $KG_PER_G;     break;
   case 
'DR'    $f $KG_PER_DR;    break;
   case 
'OZ'    $f $KG_PER_OZ;    break;
   case 
'TROZ'  $f $KG_PER_TROZ;  break;
   case 
'LB'    $f $KG_PER_LB;    break;
   case 
'TRLB'  $f $KG_PER_TRLB;  break;
   case 
'KG'    $f $KG_PER_KG;    break;
   case 
'ST'    $f $KG_PER_ST;    break;
   case 
'N'     $f $KG_PER_N;     break;
   case 
'TON'   $f $KG_PER_TON;   break;
   case 
'T'     $f $KG_PER_TONNE; break;

   default      : 
$f FALSE;         break;
  }

// ------------------------------------------
// Error if conversion factor is FALSE, which
// means that the units symbol was not found
// in the table of valid symbols.

   
if ($f === FALSE) {return $f;}

// ----------------------------------------------
// Otherwise, compute the equivalent KG value by
// multiplying the value of the argument by the
// conversion factor (f).

   
$m bcMul($x$f$Q);

// *********************************************************
// AT THIS POINT INPUT VALUE HAS BEEN CONVERTED INTO KG.
// NEXT STEP IS TO CONVERT THE KG INTO THE OUTPUT UNITS.
// *********************************************************

// --------------------------------------------------------
// Read the output units symbol argument.  The unit symbols
// are NOT case sensitive.

   
$ToUnitsSymbol StrToUpper(trim($ToUnitsStr));

// -----------------------------------------------------
// Determine the conversion factor to use from the units
// symbol argument.

   
switch ($ToUnitsSymbol)
  {
   case 
'PG'    $f $KG_PER_PG;    break;
   case 
'NG'    $f $KG_PER_NG;    break;
   case 
'UG'    $f $KG_PER_UG;    break;
   case 
'MG'    $f $KG_PER_MG;    break;
   case 
'GR'    $f $KG_PER_GR;    break;
   case 
'PWT'   $f $KG_PER_PWT;   break;
   case 
'C'     $f $KG_PER_C;     break;
   case 
'G'     $f $KG_PER_G;     break;
   case 
'DR'    $f $KG_PER_DR;    break;
   case 
'OZ'    $f $KG_PER_OZ;    break;
   case 
'TROZ'  $f $KG_PER_TROZ;  break;
   case 
'LB'    $f $KG_PER_LB;    break;
   case 
'TRLB'  $f $KG_PER_TRLB;  break;
   case 
'KG'    $f $KG_PER_KG;    break;
   case 
'ST'    $f $KG_PER_ST;    break;
   case 
'N'     $f $KG_PER_N;     break;
   case 
'TON'   $f $KG_PER_TON;   break;
   case 
'T'     $f $KG_PER_TONNE; break;
   default      : 
$f FALSE;         break;
  }

// __________________________________________
// Error if conversion factor is FALSE, which
// means that the units symbol was not found
// in the valid symbols table.

   
if ($f == FALSE) {return FALSE;}

// _____________________________________________
// Otherwise, return the equivalent of the given
// KG value by dividing the value of the
// argument by the conversion factor (f).

   
$w substr(BCDiv($m$f$Q), 0$q);

   return 
RTrim(RTrim($w'0'), '.');

// END OF  bc_Mass_To_Mass(...)

// ===========================================================================










/*
  ************************************************************************
  ************************************************************************
  This is a utility function used to generate the names of giant integers
  up to 99 digits long.
*/

   
function bc_Big_Int_Name ($BigIntStr$LableString='')
{
// Read numeric string and cut off any decimal
// digits from X without rounding.
   
$X trim($BigIntStr);
   
$X bcAdd('0'$X);

// Read optional lable string argument
// to be appended to returned result.
   
$label trim($LableString);
   if (
$label <> '') {$label $label";}

// Return empty string if X < 1 million.
//   $LenX = StrLen($X);  if (strlen($X) < 7) {return '';}
   
$LenX StrLen($X);  if ($X 1000000) {return '';}

   
$NumDigits StrLen($X);

   
$i[0]  = "";
   
$i[1]  = "thousand";
   
$i[2]  = "million";
   
$i[3]  = "billion";
   
$i[4]  = "trillion";
   
$i[5]  = "quadrillion";
   
$i[6]  = "quintillion";
   
$i[7]  = "sextillion";
   
$i[8]  = "septillion";
   
$i[9]  = "octillion";
   
$i[10] = "nonillion";
   
$i[11] = "decillion";
   
$i[12] = "undecillion";
   
$i[13] = "duodecillion";
   
$i[14] = "tredecillion";
   
$i[15] = "quattuordecillion";
   
$i[16] = "quindecillion";
   
$i[17] = "sexdecillion";
   
$i[18] = "septendecillion";
   
$i[19] = "octodecillion";
   
$i[20] = "novemdecillion";
   
$i[21] = "vigintillion";
   
$i[22] = "unvigintillion";
   
$i[23] = "duovigintillion";
   
$i[24] = "trevigintillion";
   
$i[25] = "quattuorvigintillion";
   
$i[26] = "quinvigintillion";
   
$i[27] = "sexvigintillion";
   
$i[28] = "septenvigintillion";
   
$i[29] = "octovigintillion";
   
$i[30] = "novemvigintillion";
   
$i[31] = "trigintillion";
   
$i[32] = "untrigintillion";
   
$i[33] = "duotrigintillion";

   
$j floor($NumDigits 3);

   
$k $NumDigits 3;

   if (
$k == 0)
      {
$j -= 1;  $xxx substr($X03);}
   else
      {
$xxx substr($X0$k);}

   
$w "$xxx";  $w LTrim($w'0');  if (IntVal($w) == 0) {$w '0';}

   if (
$w == '0') {return '';}

// Collect 3 digits for decimal part from X.
   
$u substr($XStrLen($w), 4) + 5;
   
$ddd SPrintF("%03d"substr($u0StrLen($u)-1));

// get length of int part

// Determine engineering notation exponent for X.
   
$E $LenX StrLen($w);

   
$w "\n$w.$ddd $i[$j]$label (E+$E)\n";

   return 
Str_Replace('.000'''$w);

// End of  bc_Big_Int_Name (BigIntStr)




?>