How can i have a integer value?

2 ビュー (過去 30 日間)
Le Dung
Le Dung 2017 年 9 月 11 日
編集済み: Le Dung 2017 年 9 月 11 日
Hi everyone. I have a problem such as: I have 2 rows that is
amount: 2010 12060 16080 2010 10050 1005
val: 0.025 0.015 0.025 0.025 0.020 0.015
and i need calculate a variable called "money":
money = amount * val
summoney = sum(moneys),
value of "summoney" is 899.475
And my problem that is: i want to value of "summoney" is equal to 900, exactly.
At here, you can change value of variables "val" while value of variables "amount" is fixed.
But, you can only chose 6 digits after dot (such as: 0.025001) or on other word, you can only round up 6 digits after dot to variable "val".
Thank you so much.

回答 (1 件)

KL
KL 2017 年 9 月 11 日
編集済み: KL 2017 年 9 月 11 日
amount = [2010 12060 16080 2010 10050 1005];
val =[0.025 0.015 0.025 0.025 0.020 0.015];
money = amount .* val;
summoney = sum(money)
d1 = (900-summoney)/amount(1) %calculate the difference to be added to val(1)
val(1)=val(1)+d1
money = amount .* val; %calculate new money and sum
summoney = sum(money)
if you want to distribute the difference across all elements of val, you can do it similarly.
  1 件のコメント
Le Dung
Le Dung 2017 年 9 月 11 日
編集済み: Le Dung 2017 年 9 月 11 日
First of All, thank you KL. of cause, it is solution that i thought, but as I said above, value of "val" must be a number that round up 6 digits after dot. That is problem. When do as you commented, val(1) = 0.025261194029851, so if i take val(1) = 0.025261, so value of summoney is not equal to 900.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by