@solana/fixed-points 6.9.0-canary-20260505230336 → 6.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -151,6 +151,27 @@ decimalFixedPointToString(usdc('42.5'), { padTrailingZeros: true }); // "42.5000
151
151
  decimalFixedPointToNumber(usdc('42.5')); // 42.5
152
152
  ```
153
153
 
154
+ For locale-aware output, pass any `Intl.NumberFormat` instance to `formatDecimalFixedPoint` or `formatBinaryFixedPoint`. The helpers route the raw bigint through string scientific notation so precision is preserved beyond JavaScript's `number` mantissa.
155
+
156
+ ```ts
157
+ const eurc = decimalFixedPoint('unsigned', 64, 6);
158
+ const eurFormatter = new Intl.NumberFormat('de-DE', { currency: 'EUR', style: 'currency' });
159
+ formatDecimalFixedPoint(eurFormatter, eurc('1234.5')); // "1.234,50 €"
160
+ ```
161
+
162
+ The same helper exists for binary fixed-points.
163
+
164
+ ```ts
165
+ const fr = new Intl.NumberFormat('fr-FR', { maximumFractionDigits: 4 });
166
+ formatBinaryFixedPoint(fr, audioSample('0.1')); // "0,1"
167
+ ```
168
+
169
+ To plug a binary fixed-point into a custom formatter, convert it to its exact base-10 representation with `binaryFixedPointToBase10` first. Decimal fixed-points already carry `raw` and `decimals` directly on the value object, so no equivalent helper is needed for them.
170
+
171
+ ```ts
172
+ binaryFixedPointToBase10(audioSample('0.5')); // { raw: 500000000000000n, decimals: 15 }
173
+ ```
174
+
154
175
  ## Type guards
155
176
 
156
177
  The `is*` and `assertIs*` guards narrow an unknown value to a fixed-point. All shape arguments are optional — pass `undefined` for any dimension you don't care about.
@@ -390,6 +390,11 @@ function gteBinaryFixedPoint(a, b) {
390
390
  }
391
391
 
392
392
  // src/binary/conversions.ts
393
+ function binaryFixedPointToBase10(value) {
394
+ const decimals = value.fractionalBits;
395
+ const raw = decimals === 0 ? value.raw : value.raw * 5n ** BigInt(decimals);
396
+ return { decimals, raw };
397
+ }
393
398
  function toUnsignedBinaryFixedPoint(value) {
394
399
  if (value.signedness === "unsigned") {
395
400
  return value;
@@ -535,11 +540,14 @@ function formatScaledBigint(raw, decimals, padTrailingZeros) {
535
540
 
536
541
  // src/binary/formatting.ts
537
542
  function binaryFixedPointToString(value, options) {
538
- const base10Decimals = value.fractionalBits;
539
- const base10Raw = base10Decimals === 0 ? value.raw : value.raw * 5n ** BigInt(base10Decimals);
540
- const { decimals, raw } = applyDecimalsOption("binaryFixedPoint", base10Raw, base10Decimals, options);
543
+ const base10 = binaryFixedPointToBase10(value);
544
+ const { decimals, raw } = applyDecimalsOption("binaryFixedPoint", base10.raw, base10.decimals, options);
541
545
  return formatScaledBigint(raw, decimals, options?.padTrailingZeros ?? false);
542
546
  }
547
+ function formatBinaryFixedPoint(formatter, value) {
548
+ const { decimals, raw } = binaryFixedPointToBase10(value);
549
+ return formatter.format(`${raw}E-${decimals}`);
550
+ }
543
551
  function binaryFixedPointToNumber(value) {
544
552
  const { fractionalBits, raw } = value;
545
553
  if (fractionalBits === 0) {
@@ -813,6 +821,9 @@ function decimalFixedPointToString(value, options) {
813
821
  const { decimals, raw } = applyDecimalsOption("decimalFixedPoint", value.raw, value.decimals, options);
814
822
  return formatScaledBigint(raw, decimals, options?.padTrailingZeros ?? false);
815
823
  }
824
+ function formatDecimalFixedPoint(formatter, value) {
825
+ return formatter.format(`${value.raw}E-${value.decimals}`);
826
+ }
816
827
  function decimalFixedPointToNumber(value) {
817
828
  return Number(value.raw) / 10 ** value.decimals;
818
829
  }
@@ -847,6 +858,7 @@ exports.addDecimalFixedPoint = addDecimalFixedPoint;
847
858
  exports.assertIsBinaryFixedPoint = assertIsBinaryFixedPoint;
848
859
  exports.assertIsDecimalFixedPoint = assertIsDecimalFixedPoint;
849
860
  exports.binaryFixedPoint = binaryFixedPoint;
861
+ exports.binaryFixedPointToBase10 = binaryFixedPointToBase10;
850
862
  exports.binaryFixedPointToNumber = binaryFixedPointToNumber;
851
863
  exports.binaryFixedPointToString = binaryFixedPointToString;
852
864
  exports.cmpBinaryFixedPoint = cmpBinaryFixedPoint;
@@ -858,6 +870,8 @@ exports.divideBinaryFixedPoint = divideBinaryFixedPoint;
858
870
  exports.divideDecimalFixedPoint = divideDecimalFixedPoint;
859
871
  exports.eqBinaryFixedPoint = eqBinaryFixedPoint;
860
872
  exports.eqDecimalFixedPoint = eqDecimalFixedPoint;
873
+ exports.formatBinaryFixedPoint = formatBinaryFixedPoint;
874
+ exports.formatDecimalFixedPoint = formatDecimalFixedPoint;
861
875
  exports.getBinaryFixedPointCodec = getBinaryFixedPointCodec;
862
876
  exports.getBinaryFixedPointDecoder = getBinaryFixedPointDecoder;
863
877
  exports.getBinaryFixedPointEncoder = getBinaryFixedPointEncoder;