fix: fix decimals problem with Chat commands
The problem was that the BigInt lib did not handle decimals at all. `divMod` only let the remainder be what is after the dot. So I created my own divde and multiply methods to move the dot depending on the number of decimals
This commit is contained in:
parent
348e0a9bdc
commit
91c8089716
|
@ -48,21 +48,13 @@ Item {
|
|||
if (!commandParametersObject.value) {
|
||||
return "0"
|
||||
}
|
||||
|
||||
// Divide the Wei value by 10^decimals
|
||||
var divModResult = Utils.newBigNumber(commandParametersObject.value)
|
||||
.divmod(Utils.newBigNumber(10)
|
||||
.pow(token.decimals))
|
||||
// Make a string with the quotient and the remainder
|
||||
var str = divModResult.quotient.toString() + "." + divModResult.remainder.toString()
|
||||
// Remove the useless zeros at the satrt and the end, but keep at least one before the dot
|
||||
return str.replace(/^(0*)([0-9\.]+?)(0*)$/g, function (match, firstZeros, whatWeKeep, secondZeros) {
|
||||
if (whatWeKeep.startsWith('.')) {
|
||||
whatWeKeep = "0" + whatWeKeep
|
||||
try {
|
||||
return Utils.divideByDecimals(commandParametersObject.value, token.decimals)
|
||||
} catch (e) {
|
||||
console.error("Error getting the ETH value of:", commandParametersObject.value)
|
||||
console.error("Error:", e.message)
|
||||
return "0"
|
||||
}
|
||||
|
||||
return whatWeKeep
|
||||
})
|
||||
}
|
||||
property string tokenSymbol: token.symbol
|
||||
property string fiatValue: {
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
software to the public domain. We make this dedication for the benefit
|
||||
of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of
|
||||
relinquishment in perpetuity of all present and future rights to this
|
||||
software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <http://unlicense.org>
|
File diff suppressed because it is too large
Load Diff
|
@ -2,12 +2,53 @@ pragma Singleton
|
|||
|
||||
import QtQuick 2.13
|
||||
import "../shared/xss.js" as XSS
|
||||
import "./BigNumber/bignumber.js" as BigNumber
|
||||
|
||||
QtObject {
|
||||
function newBigNumber(number) {
|
||||
// See here for docs: https://github.com/peterolson/BigInteger.js
|
||||
return BigNumber.bigInt(number)
|
||||
// Use this to multiply an amount by 10^decimals
|
||||
function multiplyByDecimals(amount, decimals) {
|
||||
amount = amount.toString()
|
||||
let dotIndex = -1
|
||||
for (let i = 0; i < decimals; i++) {
|
||||
dotIndex = amount.indexOf('.')
|
||||
if (dotIndex > -1) {
|
||||
if (dotIndex === amount.length - 1) {
|
||||
// The dot is at the end, we can get rid of it and add a 0
|
||||
amount = amount.substring(0, dotIndex) + "0"
|
||||
continue
|
||||
}
|
||||
// Move the dot one space to the right
|
||||
amount = amount.substring(0, dotIndex) + amount.substring(dotIndex + 1, dotIndex + 2) + "." + amount.substring(dotIndex + 2)
|
||||
continue
|
||||
}
|
||||
// Just add a new 0
|
||||
amount += "0"
|
||||
}
|
||||
|
||||
return stripStartingZeros(amount)
|
||||
}
|
||||
|
||||
// Use this to divide an amount by 10^decimals
|
||||
function divideByDecimals(amount, decimals) {
|
||||
amount = amount.toString()
|
||||
let dotIndex = amount.indexOf('.')
|
||||
if (dotIndex === -1) {
|
||||
amount = amount + "."
|
||||
}
|
||||
for (let i = 0; i < decimals; i++) {
|
||||
dotIndex = amount.indexOf('.')
|
||||
if (dotIndex === 0) {
|
||||
// The dot is at the start, we need to add a zero in front before moving it
|
||||
dotIndex++
|
||||
amount = "0" + amount
|
||||
}
|
||||
// Move the dot one position left
|
||||
amount = amount.substring(0, dotIndex - 1) + "." + amount.substring(dotIndex - 1, dotIndex) + amount.substring(dotIndex + 1)
|
||||
}
|
||||
if (amount.startsWith(".")) {
|
||||
amount = "0" + amount
|
||||
}
|
||||
|
||||
return stripTrailingZeros(amount)
|
||||
}
|
||||
|
||||
function isHex(value) {
|
||||
|
@ -91,6 +132,22 @@ QtObject {
|
|||
return strNumber.replace(/(\.[0-9]*[1-9])0+$|\.0*$/,'$1')
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes starting zeros from a string-representation of a number. Throws
|
||||
* if parameter is not a string
|
||||
*/
|
||||
function stripStartingZeros(strNumber) {
|
||||
if (!(typeof strNumber === "string")) {
|
||||
try {
|
||||
strNumber = strNumber.toString()
|
||||
} catch(e) {
|
||||
throw "[Utils.stripStartingZeros] input parameter must be a string"
|
||||
}
|
||||
}
|
||||
return strNumber.replace(/^(0*)([0-9\.]+)/, "$2")
|
||||
}
|
||||
|
||||
|
||||
function setColorAlpha(color, alpha) {
|
||||
return Qt.hsla(color.hslHue, color.hslSaturation, color.hslLightness, alpha)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue