Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way to manage conversions between multiple formats like this is to just maintain a table of conversions for one type, and do a two-part conversion to get the desired format. There is some loss of precision in doing this, but you really should have plenty of precision for the needs of something like this (you can always increase the precision by adding some more digits to the roentgen type).</p> <p>There are many ways to do this, but I created a <code>RadiationConverter</code> class to keep everything tidy, and I did a bit of chaining just to keep it clean. You get the idea, though.</p> <pre><code>&lt;?php class RadiationConverter { // these are the keys we'll reference when sending a type to the methods, // along with a value for what we'll display each type as: private $conversion_display = array ( 'sievert' =&gt; 'sievert', 'millisievert' =&gt; 'millisievert', 'microsievert' =&gt; 'microsievert', 'j/kg'=&gt; 'joule/kilogram', 'm2/s2' =&gt; 'square meter/square second', 'rem' =&gt; 'Roentgen eq. man', 'millirem' =&gt; 'millirem', 'imillicurie' =&gt; 'intensity millicurie', 'electrons'=&gt; 'gray (Wr=1, X-ray, gamma ray, electrons)', 'alpha' =&gt; 'gray (Wr=20, alpha particles)', 'roentgen'=&gt; 'Roentgen/hour (numerical equivalent)'); // this is a conversion table for sieverts to anything else // (note the conversion factor of 1 for sievert) private $conversion_table = array ( 'sievert' =&gt; 1, 'millisievert' =&gt; 0.001, 'microsievert' =&gt; 0.000001, 'j/kg' =&gt; 1, 'm2/s2' =&gt; 1, 'rem' =&gt; 0.01, 'millirem' =&gt; 0.00001, 'imillicurie' =&gt; 1, 'electrons' =&gt; 1, 'alpha' =&gt; 20, 'roentgen' =&gt; 0.119331742243, ); private $type; private $amount; private $base_type; // this is what our conversion table is based on public function __construct($from_type='sievert', $amount = 0) { $from_type = strtolower($from_type); if(!array_key_exists($from_type,$this-&gt;conversion_display)) { throw new Exception ("Radiation type does not exist."); } $this-&gt;type = $from_type; $this-&gt;amount = $amount; $this-&gt;base_type = 'sievert'; // for reference only, really } public function amount($amount=null) { if(empty($amount)) return $this-&gt;amount; $this-&gt;amount = $amount; return $this; } public function types() { return $this-&gt;conversion_display; } public function display_name($type=null) { if (empty($type)) $type=$this-&gt;type; return $this-&gt;conversion_display[$type]; } public function display_as($type){ return $this-&gt;convert_to($type) . ' '.$this-&gt;display_name($type); } public function convert_to($type) { $type = strtolower($type); if(!array_key_exists($type,$this-&gt;conversion_display)) { throw new Exception ("Radiation conversion type does not exist."); } // first we convert to our base type, then convert that to the new type: return ($this-&gt;amount*$this-&gt;conversion_table[$this-&gt;type])/$this-&gt;conversion_table[$type]; } public function base_type(){ return $this-&gt;base_type; } } </code></pre> <p>Now all you need is a couple <code>&lt;select&gt;</code> lists with option values being the various types to convert from/to, then an <code>&lt;input&gt;</code> field for an amount. You could use it like this:</p> <pre><code>$converter = new RadiationConverter($_POST['convert_from']); // return just the number echo $converter-&gt;amount($_POST['amount'])-&gt;convert_to($_POST['convert_to']); // or return the number with the new type echo $converter-&gt;amount($_POST['amount'])-&gt;display_as($_POST['convert_to']); // you can include the amount in the construct if you want like this: $converter = new RadiationConverter('rem', 100); echo $converter-&gt;display_as('electrons'); </code></pre> <p>If you want to create a table like on that site:</p> <pre><code>$display_amount = $_POST['amount'] . " " . $converter-&gt;display_name(); // just take the convert_from variable and cycle through all the types: foreach($converter-&gt;types() as $type =&gt; $display) { echo "$display_amount = " . $converter-&gt;display_as($type) . "&lt;br /&gt;"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload