-2

I am working on a web-based application build with Codeigniter.

Part of the user form:

<div class="form-group" id="hasil_swab_tpc">
  <label for="swab_tpc" class="col-sm-3 control-label">Hasil TPC</label>
  <div class="col-sm-8">
    <input type="text" id="swab_tpc" name="swab_tpc" class="form-control" placeholder="Hasil Analisa TPC">
  </div>
</div>

I want the user input normal value, let say 250, save the value to the database. Then in the view page, I want to display that value in scientific notation 2.5 x 102.

My question is: how to convert a number to the scientific notation? For some reason, I need to display the number from user input from 250 to 2.5 x 102 and 25 to 2.5 x 101, etc.

YakovL
  • 7,557
  • 12
  • 62
  • 102
Firman
  • 463
  • 2
  • 7
  • 21
  • from wikipedia: Decimal to scientific First, move the decimal separator point sufficient places, n, to put the number's value within a desired range, between 1 and 10 for normalized notation. If the decimal was moved to the left, append "× 10n"; to the right, "× 10−n". To represent the number 1,230,400 in normalized scientific notation, the decimal separator would be moved 6 digits to the left and "× 106" appended, resulting in 1.2304×106. The number −0.0040321 would have its decimal separator shifted 3 digits to the right instead of the left and yield −4.0321×10−3 as a result. – Vickel Jan 29 '18 at 20:24
  • https://en.wikipedia.org/wiki/Scientific_notation see "Converting Numbers Decimal to scientific" – Vickel Jan 29 '18 at 20:25
  • so is your question about converting or about displaying? If only about converting, please remove the `codeigniter` tag and describe what's the input (have you already got a "float" value? or you have also some problems with parsing a string input?). Please show what you have so far – YakovL Jan 29 '18 at 21:02
  • @YakovL actually I want to display the value from database into the view page as a scientific notation. I've updated the question. – Firman Jan 31 '18 at 06:15

1 Answers1

0

Well, this has nothing to do with CodeIgniter itself; practically what you're asking about is – how to get the order of magnitude? Because once you have it, you can echo stuff like

$value / pow(10,$order_of_magnitude) . ' x 10<sup>' . $order_of_magnitude . '</sup>'

(or use interpolation)

Now, why don't you calc it in a straightforward way? Like this:

function getOrderOfMagnitude($value) {
    if($value == 0)
        return null; // you have to deal with this separately when displaying the value
    $absValue = abs($value); // let's deal with non-negative values only
    $orderOfMagnitude = 0;

    while($absValue >= 10) { // keep in mind that these "=" cases are limited to how the arithmetic works (see https://stackoverflow.com/q/588004/3995261)
        $absValue /= 10;
        $orderOfMagnitude++;
    }
    while($absValue < 0.1) {
        $absValue *= 10;
        $orderOfMagnitude--;
    }

    return $orderOfMagnitude;
}

This should be enough for your needs.

YakovL
  • 7,557
  • 12
  • 62
  • 102