Commit 44bbf8d9 authored by zhijie.xue's avatar zhijie.xue

概率用百分比展示

parent 92ce716d
function getCommonDivisor(firstNum, secondNum) { // function getCommonDivisor(firstNum, secondNum) {
if (secondNum === 0) return firstNum; // if (secondNum === 0) return firstNum;
var remainder = parseInt(firstNum % secondNum); // var remainder = parseInt(firstNum % secondNum);
return getCommonDivisor(secondNum, remainder); // return getCommonDivisor(secondNum, remainder);
} // }
function getProbability (quantity, coinQuantity) { // function getProbability (quantity, coinQuantity) {
// 如果用户克币数是0 // // 如果用户克币数是0
if (quantity === 0) { // if (quantity === 0) {
return '0'; // return '0';
} // }
var divisor = getCommonDivisor(quantity, coinQuantity); // var divisor = getCommonDivisor(quantity, coinQuantity);
var value = quantity / divisor; // var value = quantity / divisor;
var value2 = coinQuantity / divisor; // var value2 = coinQuantity / divisor;
// 如果分子比分母大说明中奖概率是100%,那么返回中奖概率 // // 如果分子比分母大说明中奖概率是100%,那么返回中奖概率
if (value >= value2) { // if (value >= value2) {
// return '100%';
// } else {
// return value + '/' + value2;
// }
// }
function getProbabilityByPercent(quantity, coinQuantity) {
if (quantity === 0) return '0%';
const quotient = quantity / coinQuantity;
if (quotient < 0.01) {
return '0.01%';
} else if (quotient >= 1) {
return '100%'; return '100%';
} else { } else {
return value + '/' + value2; const reg = new RegExp('\\.?0+$');
let str = quotient.toFixed(2);
return str.replace(reg, '') + '%';
} }
} }
...@@ -56,6 +71,6 @@ function checkUpdateVersion() { ...@@ -56,6 +71,6 @@ function checkUpdateVersion() {
}); });
} }
module.exports = { module.exports = {
getProbability, getProbability: getProbabilityByPercent,
checkUpdateVersion checkUpdateVersion
}; };
...@@ -4,8 +4,22 @@ function getCommonDivisor(firstNum, secondNum) { ...@@ -4,8 +4,22 @@ function getCommonDivisor(firstNum, secondNum) {
return getCommonDivisor(secondNum, remainder); return getCommonDivisor(secondNum, remainder);
} }
module.exports = { function getProbabilityByPercent(quantity, coinQuantity) {
getProbability: function (quantity, coinQuantity) { if (quantity === 0) return '0%';
var quotient = quantity / coinQuantity;
if (quotient < 0.01) {
return '0.01%';
} else if (quotient >= 1) {
return '100%';
} else {
var reg = getRegExp('\\.?0+$')
var str = quotient.toFixed(2);
return str.replace(reg, '') + '%';
}
}
function getProbability(quantity, coinQuantity) {
if (!quantity) return 0; if (!quantity) return 0;
// 如果用户克币数是0 // 如果用户克币数是0
if (quantity === 0) { if (quantity === 0) {
...@@ -21,4 +35,7 @@ module.exports = { ...@@ -21,4 +35,7 @@ module.exports = {
return value + '/' + value2; return value + '/' + value2;
} }
} }
module.exports = {
getProbability: getProbabilityByPercent
}; };
...@@ -55,26 +55,3 @@ export function compressShareImage(url) { ...@@ -55,26 +55,3 @@ export function compressShareImage(url) {
} }
return url; return url;
} }
function getCommonDivisor(firstNum, secondNum) {
if (secondNum === 0) return firstNum;
var remainder = parseInt(firstNum % secondNum);
return getCommonDivisor(secondNum, remainder);
}
export function getProbability(quantity, coinQuantity) {
if (!quantity) return 0;
// 如果用户克币数是0
if (quantity === 0) {
return '0%';
}
var divisor = getCommonDivisor(quantity, coinQuantity);
var value = quantity / divisor;
var value2 = coinQuantity / divisor;
// 如果分子比分母大说明中奖概率是100%,那么返回中奖概率
if (value >= value2) {
return '100%';
} else {
return value + '/' + value2;
}
}
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
<script> <script>
import wepy from '@wepy/core'; import wepy from '@wepy/core';
import { getAwardsDetail, getPrizeList } from '../../common/api.js'; import { getAwardsDetail, getPrizeList } from '../../common/api.js';
import {getProbability} from '../../common/utils'; import {getProbability} from '../../common/raffleProbability';
wepy.component({ wepy.component({
props: { props: {
videoList: { videoList: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment