计算两个数字之间的百分比变化

时间:2017-10-03 13:36:10

标签: php math

寻找关于以下代码的一些说明,用于计算两个不同数量之间的百分比变化,其中原始数字可以是更大或更小的数字。那么这段代码是否可以显示增加+或减少 - 更改?感谢。

$original= 100;
$current = 95;

$percentChange = (1 - $original / $current ) * 100;

3 个答案:

答案 0 :(得分:3)

找出差异,然后像这样计算百分比

<?php
    $original= 100;
    $current = 115;
    $diff = $current - $original;
    $more_less = $diff > 0 ? "More" : "Less";
    $diff = abs($diff);
    $percentChange = ($diff/$original)*100;
    echo "$percentChange% $more_less agaist $original";
?>

11090100

的差异相同

现场演示:https://eval.in/872926

答案 1 :(得分:1)

此函数更有用,因为它可以防止被零除,输出是可取整的,并且可以处理正(增加)和负(减少)返回值。

    const app = new PIXI.Application({
      width: window.innerWidth,
      height: window.innerHeight,
      antialias: true,
      resizeTo: window,
      transparent: true
    });
    
    document.body.appendChild(app.view);
    
    //Image
    const vertexShader = `
      precision mediump float;
    
      attribute vec2 aVertexPosition;
      attribute vec2 aSize;
    
      uniform mat3 translationMatrix;
      uniform mat3 projectionMatrix;
    
      varying vec2 vUvs;
    
      uniform vec2 uSize;
    
      void main() {
        vUvs = vec2(aVertexPosition.x / uSize.x, aVertexPosition.y / uSize.y);
        gl_Position = vec4((projectionMatrix * translationMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);
      }
    `;

    const fragmentShader = `
      precision mediump float;
    
      varying vec2 vUvs;
    
      uniform sampler2D uTexture;
    
      void main() {
        gl_FragColor = texture2D(uTexture, vUvs);
       }
      `;
    
    const width = 1400;
    const height = 300;
    
    const uniforms = {
      uTexture: PIXI.Texture.from(imageUrl),
      uSize: [width, height],
      uTime: 0
    };
    const shader = PIXI.Shader.from(vertexShader, fragmentShader, uniforms);
    const mesh = new PIXI.Mesh(new PIXI.PlaneGeometry(width, height, 32, 32), shader);
    mesh.position.y = 100;
    
    app.stage.addChild(mesh);

答案 2 :(得分:0)

是的,它可用于显示增加+或减少 - 更改。