Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# STDLIB_VERSION 3 #-}
- {-# SCRIPT_TYPE ACCOUNT #-}
- {-# CONTENT_TYPE DAPP #-}
- # Waves RIDE Example: Integer to Float Number as String
- #
- # https://waves-dapp.com/3P3rCd4SuTSjgxjbd9FgV9eduDi5A74iMT7
- # https://wavesexplorer.com/tx/FA5zQtcNHoFZDSeNwbznhBGrS19d6JTzKDfX53XC77Rs
- # https://wavesexplorer.com/address/3P3rCd4SuTSjgxjbd9FgV9eduDi5A74iMT7/data
- # Copyright © 2020 John Silver aka. Джон Ведьмолов, <https://t.me/dex_investments>.
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU Affero General Public License as
- # published by the Free Software Foundation, either version 3 of the
- # License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU Affero General Public License for more details.
- #
- # You should have received a copy of the GNU Affero General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
- # The function returns a floating point number, separated by thousands.
- # rus: Функция возвращает число с плавающей точкой, разделенное тысячами.
- func toFloatString(num: Int, mult: Int) = {
- let th = "," # thousands sign
- let dp = "." # decimal point sign
- # The function separate thousands with commas.
- func joinThousands(acc: String, val: Int) = {
- if (acc == "" && val == 0) then ""
- else if (acc == "") then toString(val)
- else acc + th + toString(val + 1000).drop(1)
- }
- # The function remove trailing fractional zeros.
- func dropRightZero(str: String) = {
- if (str.drop(str.size() - 1) != "0")
- then str else str.take(str.size() - 1)
- }
- #let abs = if (num < 0) then -num else num
- let ip = num / mult # integer part
- let fp = num % mult # fractional part
- # abbr: qN - quotient, rN - remainder
- # max integer: 9,223,372,036,854,775,807
- let q6 = ip / (1000 * 1000 * 1000 * 1000 * 1000 * 1000)
- let r6 = ip % (1000 * 1000 * 1000 * 1000 * 1000 * 1000)
- let q5 = r6 / (1000 * 1000 * 1000 * 1000 * 1000)
- let r5 = r6 % (1000 * 1000 * 1000 * 1000 * 1000)
- let q4 = r5 / (1000 * 1000 * 1000 * 1000)
- let r4 = r5 % (1000 * 1000 * 1000 * 1000)
- let q3 = r4 / (1000 * 1000 * 1000)
- let r3 = r4 % (1000 * 1000 * 1000)
- let q2 = r3 / (1000 * 1000)
- let r2 = r3 % (1000 * 1000)
- let q1 = r2 / (1000)
- let r1 = r2 % (1000) # q0 = r1 / 1
- # abbr: is - integer part as string, fs - fractional part as string
- let is = FOLD<7>([q6, q5, q4, q3, q2, q1, r1], "", joinThousands)
- let fs = toString(fp + mult).drop(1).take(8)
- # Uncomment to remove trailing fractional zeros.
- #.dropRightZero().dropRightZero().dropRightZero().dropRightZero()
- #.dropRightZero().dropRightZero().dropRightZero().dropRightZero()
- #(if (num < 0) then "-" else "") +
- (if (is != "") then is else "0") + (if (fs != "") then (dp + fs) else "")
- }
- @Callable(inv)
- func setFloatData(keyName: String, intValue: Int,
- decPlaces: Int, isDollar: Boolean) = {
- if (inv.caller != this) then
- # rus: Только владелец может изменить данные!
- throw("Only the owner can change the data!")
- else if (intValue < 0 || decPlaces < 0) then
- # rus: Отрицательные числа не допускаются.
- throw("Negative numbers are not allowed.")
- else if (decPlaces > 8) then
- # rus: Десятичные знаки находятся вне предела 0-8.
- throw("Decimal places are outside the range of 0-8.")
- else if (isDollar && decPlaces != 0 && decPlaces != 2) then
- # rus: Допустимые десятичные знаки для доллара: 0 или 2.
- throw("Valid decimal places for the dollar: 0 or 2.")
- else {
- let prefix = if isDollar then "$" else ""
- let decMult = parseIntValue("100000000".take(decPlaces + 1))
- WriteSet([
- DataEntry(keyName, prefix + intValue.toFloatString(decMult))
- ])
- }
- }
- @Verifier(tx)
- func verify() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement