codeboutique/price-rounder

0.3.0 2017-09-25 09:22 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:41:34 UTC


README

build status Latest Stable Version Total Downloads License

PriceRounder

PriceRounder是一个价格四舍五入库,可以生成带有范围支持的美观价格(漂亮的价格)。用于在将价格从其他货币转换时生成美观的价格。

附带一个最佳价格策略 BestPriceRounder,它为大多数金额提供最佳美观价格,而不管金额大小。它可以配置以包含美观价格的类型

  • 9分或1分美观价格,如5.99或5.49 INCLUDE_NINES
  • 95分或5分美观价格,如5.95 INCLUDE_NINETYFIVES
  • 8分(对于迷信的人),如498或3.48 INCLUDE_EIGHTS
  • 一半,如4.5或550 INCLUDE_HALVESINCLUDE_HALVES

默认情况下包括9分、98分和一半 INCLUDE_DEFAULT

如果您想有更具体的控制,其他Rounder也都可以使用。

  • HalvesRounder
  • CentsRounder
  • NinesRounder 扩展 CentsRounder
  • NinetyfiveRounder 扩展 CentsRounder

这些可以通过RangeRounder结合使用,以便为不同的范围使用不同的四舍五入器。

还可以执行简单且可扩展的格式化:请参阅Rounder类。

示例

实例化一个四舍五入器并调用round方法。就这样

$rounder = new NinetyfiveRounder();
$rounder->round(15.12); // returns 14.95

最佳价格示例

$rounder = new BestPriceRounder();
$rounder->round(66.33); // return 66.49

$rounder->printAnalysis(66.33);

+----------------------+-----------+------+------------+------+-------+---+
| Candidate for: 66.33 | Base Diff |    % | Total Diff |    % | Score |   |
+----------------------+-----------+------+------------+------+-------+---+
|                65.95 |        -1 | -1.5 |      -0.38 | -0.6 |  0.22 |   |
|                65.99 |        -1 | -1.5 |      -0.34 | -0.5 |  0.18 |   |
|                66.49 |         0 |  0.0 |       0.16 |  0.2 |  0.04 | * |
|                 66.5 |         0 |  0.0 |       0.17 |  0.3 |  0.04 |   |
|                66.95 |         0 |  0.0 |       0.62 |  0.9 |  0.57 |   |
|                66.99 |         0 |  0.0 |       0.66 |  1.0 |  0.65 |   |
|                   67 |         1 |  1.5 |       0.67 |  1.0 |  0.67 |   |
+----------------------+-----------+------+------------+------+-------+---+

$rounder->printAnalysis(1253.2);

+-----------------------+-----------+------+------------+------+--------+---+
| Candidate for: 1253.2 | Base Diff |    % | Total Diff |    % |  Score |   |
+-----------------------+-----------+------+------------+------+--------+---+
|                  1195 |       -58 | -4.6 |     -58.20 | -4.9 | 283.45 |   |
|                  1199 |       -54 | -4.3 |     -54.20 | -4.5 | 245.01 |   |
|                  1249 |        -4 | -0.3 |      -4.20 | -0.3 |   1.41 |   |
|                  1250 |        -3 | -0.2 |      -3.20 | -0.3 |   0.82 | * |
|                  1295 |        42 |  3.4 |      41.80 |  3.2 | 134.92 |   |
|                  1299 |        46 |  3.7 |      45.80 |  3.5 | 161.48 |   |
|                  1300 |        47 |  3.8 |      46.80 |  3.6 | 168.48 |   |
+-----------------------+-----------+------+------------+------+--------+---+

最佳值评分考虑价格的最小变化。默认情况下,价格四舍五入器首先倾向于奇数,然后是盈利(或避免损失)。这可以在创建四舍五入器对象时进行配置。

$rounder = new BestPriceRounder([
    "halves": 3.0,
    "gain": 10.0
]);

这将优先考虑以五结尾的值——4.5或450,但最重要的是,它会优先考虑不会造成损失的价格。

请参阅example.php中的更多示例

+----------+----------+--------+-------------------+--------------------+
| Original | No edges | odd: 9 | gain: 9, nines: 5 | eights: 5, gain: 5 |
+----------+----------+--------+-------------------+--------------------+
| 5.1      | 4.99     | 5.48   | 5.49              | 5.48               |
| 5.23     | 5.48     | 5.48   | 5.49              | 5.48               |
| 5.6      | 5.5      | 5.5    | 5.49              | 5.48               |
| 6.1      | 5.99     | 5.99   | 5.99              | 5.98               |
| 9.6      | 9.5      | 9.5    | 9.49              | 9.48               |
| 14.51    | 14.49    | 14.49  | 14.49             | 14.49              |
| 27.53    | 27.5     | 27.5   | 27.5              | 27.5               |
| 66.33    | 66.48    | 66.48  | 66.49             | 66.48              |
| 512      | 499      | 499    | 549               | 498                |
| 752      | 749      | 749    | 749               | 749                |
| 1253.2   | 1249     | 1249   | 1249              | 1248               |
| 2502.33  | 2499     | 2499   | 2499              | 2499               |
| 2517.33  | 2499     | 2499   | 2549              | 2548               |
+----------+----------+--------+-------------------+--------------------+

范围示例

$rounder = new RangeRounder([
    10  => "NinesRounder",
    50  => "NinetyfiveRounder",
    150 => "NinetyfiveRounder::1",
    999 => "NinesRounder::2",
    "*" => "round"   // uses regular PHP function through function_exists()?
]);

$rounder->round(5.6); // returns 5.99
$rounder->round(6.1); // returns 5.99
$rounder->round(9.6); // returns 9.99
$rounder->round(10.3); // returns 9.95
$rounder->round(14.49); // returns 13.95
$rounder->round(14.51); // returns 14.95
$rounder->round(50); // returns 49.95
$rounder->round(75); // returns 79.50
$rounder->round(79); // returns 79.50
$rounder->round(83); // returns 79.50
$rounder->round(92); // returns 89.50
$rounder->round(99); // returns 99.50
$rounder->round(512); // returns 499
$rounder->round(752); // returns 799
$rounder->round(982); // returns 999
$rounder->round(1100); // returns 1100
$rounder->round(2222); // returns 2222
$rounder->round(2502.33); // returns 2502

在上面的示例中,四舍五入器是根据字符串实例化的,但您也可以使用预先配置的四舍五入器实例。

字符串格式

Rounder::param[|param2 ...][::[decimals[|dec_point[|thousand_sep]]]]

例如。

"Rounder::::2"    // with value of 5 will output 5.00
"Rounder::::2|,"  // with value of 5 will output 5,00 // changed decimal point to comma
"NinetyfiveRounder"    // with value of 92 will output 92.95
"NinetyfiveRounder::1" // with value of 92 will output 89.5 // changed factor value

注意:如果您使用从Rounder继承的自定义四舍五入器,请务必包含命名空间。