('') Feet (') ve inç m (metre) bir dönüşüm Prettifying

3 Cevap php

Ben bir kişinin BMI tanımlamak için tablo oluşturma. (Ben bir şey iş tek başına, ilk yapmak istiyorum çünkü) (henüz) grafik girişi almaz, ancak (paralel eksen üzerinde) metre ve feet / inç hem de yükseklik gösterisi yapar.

Bunu yapmak için ben metre 'başlangıç ​​noktası ve aralık tanımlanması ve sonra ben (lütfen yapmak değil laugh ile geldim, hangi yapmak, fit / inç içine tanımlanmış metre değişkenleri dönüştürerek. ..) Aşağıdaki:

<?php
        $m; // height in m

        $hInInches = ($m*3.2808399)*12;

        $hInImp = explode(".",$hInInches);

        $hInFt = $hInImp[0];

        $hInInches = substr(12*$hInImp[1],0,2);
?>

Herkes bu (Elswhere tanımlı) satır x numaraları oluşturmak için () döngü için bir içinde çalıştırmak ediliyor çünkü bu, yapılabilir hangi herhangi bir güzel, daha ekonomik, daha doğru araçlara sahip merak ediyordum , ve ben yükünü azaltmak için (eğer mümkünse) istiyorum ...

3 Cevap

İşte bir yaklaşım psuedo kodu olduğunu:

inches_per_meter = 39.3700787
inches_total = round(meters * inches_per_meter)  /* round to integer */
feet = inches_total / 12  /* assumes division truncates result; if not use floor() */
inches = inches_total % 12 /* modulus */

Siz de bir sabite 12 çekin olabilir ...

To me you should avoid the string manipulation functions as derobert already stated. In php the code should be similar to the following one:

<?php
   $m=2; // height in m
    $hInFeet= $m*3.2808399;
$hFeet=(int)$hInFeet; // truncate the float to an integer
    $hInches=round(($hInFeet-$hFeet)*12); 
?>

Sadece iki çarpma ve çıkarma (artı yuvarlamak için bir işlev çağrısı) oldukça ekonomik, ve kod da oldukça okunabilir.

Sana this güzel düşünün emin değilim; Ancak, ben bir ajax/javascript çözüm fikir olabileceğini iddia ediyorum. Kullanıcı değerini girerken, sonuçlar güncelleyin.

with regards to your code
* define M_TO_FEET, FEET_TO_INCH constants.
* define 2 equations feet_to_metres(value_to_convert) and metres_to_feet(value_to_convert)
* write the conversion code in each and let it return the result

and then you can create a simple if statement:
* If user inputs metres, then metres_to_feet(value_entered_by_user)