它看起來像你的領域amo_intr
的類型爲int。
我嘗試用這個PHP腳本
<?php
date_default_timezone_set('America/Los_Angeles');
$nn=11;
$install=16.14;
$edat='2016-03-15';
$amt=100;
$i=0.12;
$actual_l='L8080';
$ecn='R555555';
$amo_intr=$amt*$i;
echo ("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$edat','".(int)$amo_intr."','$actual_l','$ecn','$amt')")."\n";
$lt=0;
while($lt<$nn)
{
$lt++;
$balance=$amt-($install-$amo_intr);
$amo_intr=$balance*$i;
$otherdat = strtotime(date("Y-m-d", strtotime($edat)) . " +".$lt." month");
$otherdat = date("Y-m-d",$otherdat);
echo ("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$otherdat','".(int)$amo_intr."','$actual_l','$ecn','$amt')")."\n";
}
?>
到simultate,並得到這樣的結果
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-03-15','12','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-04-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-05-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-06-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-07-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-08-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-09-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-10-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-11-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-12-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2017-01-15','11','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2017-02-15','11','L8080','R555555','100')
也有你的$公式中的錯誤amo_intr計算:
- 你需要在while循環前初始化餘額
$balance = $amt;
- 並計算內部循環餘額
$balance-=($install-$amo_intr);
工作代碼爲你:
<?php
date_default_timezone_set('America/Los_Angeles');
$nn=11;
$install=16.14;
$edat='2016-03-15';
$amt=100;
$i=0.12;
$actual_l='L8080';
$ecn='R555555';
$amo_intr=$amt*$i;
echo ("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$edat','".$amo_intr."','$actual_l','$ecn','$amt')")."\n";
$balance = $amt;
$lt=0;
while($lt<$nn)
{
$lt++;
$balance-=($install-$amo_intr);
$amo_intr=$balance*$i;
$otherdat = strtotime(date("Y-m-d", strtotime($edat)) . " +".$lt." month");
$otherdat = date("Y-m-d",$otherdat);
echo ("insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('$otherdat','".$amo_intr."','$actual_l','$ecn','$amt')")."\n";
}
?>
隨着輸出
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-03-15','12','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-04-15','11.5032','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-05-15','10.946784','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-06-15','10.32359808','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-07-15','9.6256298496','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-08-15','8.843905431552','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-09-15','7.9683740833382','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-10-15','6.9877789733388','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-11-15','5.8895124501395','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2016-12-15','4.6594539441562','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2017-01-15','3.281788417455','L8080','R555555','100')
insert into amortize(amo_dat,amo_intr,lcode,ecn,amt) values('2017-02-15','1.7388030275496','L8080','R555555','100')
你是什麼'amo_intr'列的數據類型? – eggyal
哦,順便說一句 - 「*不要評論關於mysql語法,我知道它的depricated *」......它簡單地將mysql換成mysqli比輸入那9個單詞更容易。 – eggyal
amo_intr是十進制的 – Leereal