JAVA amounts are apportioned proportionally, with zero Header Processing and java amounts allocated with zero Header

Source: Internet
Author: User

JAVA amounts are apportioned proportionally, with zero Header Processing and java amounts allocated with zero Header

  • Exact amount calculation is requiredBigDecimal;
  • Evenly distributed, with a fraction of the allocation, usually composed of "precision" andApportionment CoefficientDetermined;
  • HowZero HeaderTo ensureAverage as much as possibleAllocate.

1. Shared by households

/*** Assignment by number of households ** objective: to allocate a specified amount of RMB 200 to six households on average, with the same amount before and after the assignment */private static void hsFt () {BigDecimal [] repair_shou_Amt = new BigDecimal [6]; // The service amount BigDecimal fact_repair_Amt = new BigDecimal ("0") for each sub-account; // The actual fee (excluding the null value) // The amount to be apportioned: BigDecimal ft_amt = new BigDecimal ("200"); // the total number of households: int count = repair_shou_Amt.length; for (int I = 0; I <count; I ++) {// calculate the undertaking amount of each sub-account = apportioned amount/total number of accountsRepair_shou_Amt [I] = ft_amt.divide (new BigDecimal (count), 2, BigDecimal. ROUND_DOWN );// Actual total apportioned amount --- total amount after decimal point truncation fact_repair_Amt = fact_repair_Amt.add (repair_shou_Amt [I]);} // fraction = apportioned actual undertaking amount-amount to be apportioned BigDecimal repair_oddment_amt = fact_repair_Amt.subtract (ft_amt); // calculates the number of accounts to be apportioned to the fraction (accuracy: 0.01, so * 100)Int repair_int = repair_oddment_amt.multiply (new BigDecimal ("100"). abs (). intValue ();// Distribute the repair amount to the sub-accounts. if the number of sub-accounts involved in the repair is greater than zero or less than the total number, allocate if (repair_int <count) & (repair_int> 0) {// If the fraction is smaller than zero, it indicates that the amount of the repair_int sub-households is less than 0.01 RMB, add a penny if (repair_oddment_amt.compareTo (new BigDecimal ("0") <0) {for (int I = 0; I <repair_int; I ++) {repair_shou_Amt [I] = repair_shou_Amt [I]. add (new BigDecimal ("0.01") ;}} else {// If the zero header is greater than zero, it indicates that the amount of one sub-account with a repair_int is 0.01 more than one cent, you need to deduct a penny for (int I = 0; I <repair_int; I ++) {r Epair_shou_Amt [I] = repair_shou_Amt [I]. subtract (new BigDecimal ("0.01") ;}} else if (repair_int = 0) {// if the zero header is zero, indicates the amount has been evenly distributed} else {throw new BusinessException ("invalid operation! ");}}

2. Evenly distributed by area

/*** Area-based allocation method * objective: to allocate a specified amount of RMB 200 to six households on average. The area of each household may vary, and the amount before and after the allocation is equal */public static void areaFt () {BigDecimal [] repair_shou_Amt = new BigDecimal [6]; // The service amount BigDecimal fact_repair_Amt = new BigDecimal ("0 "); // actual cost of the service object (excluding the first item) // The amount to be apportioned BigDecimal ft_amt = new BigDecimal ("200"); // the total number of households int count = repair_shou_Amt.length; // The household area is BigDecimal [] info_area = {new BigDecimal ("20.12"), new BigDecimal ("10.12"), new BigDecimal ("11.12 "), new BigDecimal ("15.12"), new BigDecimal ("10.12"), new BigDecimal ("110.12")}; // The allocated total area BigDecimal totalArea = ObjectUtil. getZeroBigDecimal (); for (int I = 0; I <count; I ++) {totalArea = totalArea. add (info_area [I]) ;}for (int I = 0; I <count; I ++) {BigDecimal oa_hou_area = info_area [I]; // calculate the undertaking amount for each sub-household = (sub-household area/total construction area) * apportioned amountRepair_shou_Amt [I] = (oa_hou_area.multiply (ft_amt). divide (totalArea, 2, BigDecimal. ROUND_DOWN ));// Actual total apportioned amount --- total amount after decimal point truncation fact_repair_Amt = fact_repair_Amt.add (repair_shou_Amt [I]);} // fraction = apportioned actual undertaking amount-amount to be apportioned BigDecimal repair_oddment_amt = fact_repair_Amt.subtract (ft_amt); // calculates the number of accounts to be apportioned to the fractionInt repair_int = repair_oddment_amt.multiply (new BigDecimal ("100"). abs (). intValue ();// Distribute the repair amount to the sub-accounts. if the number of sub-accounts involved in the repair is greater than zero or less than the total number, the sub-accounts will be apportioned. Otherwise, if (repair_int <count) is reported) & (repair_int> 0) {// If the fraction is smaller than zero, it indicates that the amount of the repair_int sub-households is less than 0.01 RMB, add a penny if (repair_oddment_amt.compareTo (new BigDecimal ("0") <0) {for (int I = 0; I <repair_int; I ++) {repair_shou_Amt [I] = repair_shou_Amt [I]. add (new BigDecimal ("0.01") ;}} else {// If the zero header is greater than zero, it indicates that the amount of one sub-account with a repair_int is 0.01 more than one cent, you need to deduct a penny for (int I = 0; I <repair_int; I + +) {Repair_shou_Amt [I] = repair_shou_Amt [I]. subtract (new BigDecimal ("0.01") ;}} else if (repair_int = 0) {// if the zero header is zero, no need to handle} else {throw new BusinessException ("invalid operation! ");}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.