mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 05:38:03 +00:00
ec06da0ebc
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
31 lines
942 B
TypeScript
31 lines
942 B
TypeScript
// Copyright 2017-2025 @polkadot/util authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
interface Options {
|
|
bitLength?: 32 | 64;
|
|
isLe?: boolean;
|
|
}
|
|
|
|
/**
|
|
* @name floatToU8a
|
|
* @description Converts a float into a U8a representation (While we don't use BE in SCALE
|
|
* we still allow for either representation, although, as elsewhere, isLe is default)
|
|
*/
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
export function floatToU8a (value: String | string | number | Number = 0.0, { bitLength = 32, isLe = true }: Options = {}): Uint8Array {
|
|
if (bitLength !== 32 && bitLength !== 64) {
|
|
throw new Error('Invalid bitLength provided, expected 32 or 64');
|
|
}
|
|
|
|
const result = new Uint8Array(bitLength / 8);
|
|
const dv = new DataView(result.buffer, result.byteOffset);
|
|
|
|
if (bitLength === 32) {
|
|
dv.setFloat32(0, Number(value), isLe);
|
|
} else {
|
|
dv.setFloat64(0, Number(value), isLe);
|
|
}
|
|
|
|
return result;
|
|
}
|