Skip to content

Commit

Permalink
problem: DOT has outdated units
Browse files Browse the repository at this point in the history
  • Loading branch information
splix committed Sep 7, 2020
1 parent 1ac36e1 commit c8e3d91
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 104 deletions.
8 changes: 4 additions & 4 deletions docs/01-common-types.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ address = new Address(SS58Type.Network.LIVE, getPubKey());
// create amount of 100,000 Plancks
DotAmount amount = DotAmount.fromPlancks(100_000);
// create 4.25 Dots, which is 4.25 * 10^12 == 4250000000000 Plancks
// create 4.25 Dots, which is 4.25 * 10^10 == 42500000000 Plancks
amount = DotAmount.fromDots(4.25);
----

Expand All @@ -54,7 +54,7 @@ Not the auto formatter chooses Millidots as the optimal unit to display the valu
----
amount = amount
.divide(1000)
.add(DotAmount.fromDots(0.0012));
.add(DotAmount.fromDots(0.00112));
System.out.println(
DotAmountFormatter.autoFormatter().format(amount)
Expand Down Expand Up @@ -85,7 +85,7 @@ System.out.println(
It would print:

----
12345600000 Planck
123456000 Planck
----

If you don't want to see such large values in Plancks, you can use `autoFormatter` which will find an optimal size for the passed value:
Expand Down Expand Up @@ -152,7 +152,7 @@ System.out.println(
Which would print:

----
12,345,600,000.0000 (of uDOT)
123,456,000.0000 (of uDOT)
----

== Hash256 and Hash512
Expand Down
2 changes: 1 addition & 1 deletion examples/common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Common config for Polkaj Examples
//
ext {
polkajVersion = "0.3.0"
polkajVersion = "0.4.0-SNAPSHOT"
}

repositories {
Expand Down
8 changes: 4 additions & 4 deletions examples/types/src/main/java/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static void dotAmount() {
// create amount of 100,000 Plancks
DotAmount amount = DotAmount.fromPlancks(100_000);

// create 4.25 Dots, which is 4.25 * 10^12 == 4250000000000 Plancks
// create 4.25 Dots, which is 4.25 * 10^10 == 42,500,000,000 Plancks
amount = DotAmount.fromDots(4.25);

// print formatted value
Expand All @@ -31,7 +31,7 @@ static void dotAmount() {
// and then add 0.00112 Dot (or 1.12 Millidot)
amount = amount
.divide(1000)
.add(DotAmount.fromDots(0.0012));
.add(DotAmount.fromDots(0.00112));

// and print the result
// prints:
Expand All @@ -47,7 +47,7 @@ static void dotAmountFormat() {
DotAmountFormatter formatter = DotAmountFormatter.fullFormatter();

// Prints:
// 12345600000 Planck
// 123456000 Planck
System.out.println(
formatter.format(DotAmount.fromDots(0.0123456))
);
Expand Down Expand Up @@ -85,7 +85,7 @@ static void dotAmountFormat() {
.build();

// Prints
// 12,345,600,000.0000 (of uDOT)
// 123,456,000.0000 (of uDOT)
System.out.println(
formatter.format(DotAmount.fromDots(0.0123456))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ public class DotAmount implements Comparable<DotAmount>{
/**
* Standard units for DOT token
*
* Planck -&gt; Point -&gt; Microdot -&gt; Millidot -&gt; Dot
* Planck -&gt; Microdot -&gt; Millidot -&gt; Dot
*/
public static final Units Polkadots = new Units(
Units.Planck,
Units.Point,
Units.Microdot,
Units.Millidot,
Units.Dot
Expand Down Expand Up @@ -144,7 +143,7 @@ public Units getUnits() {

/**
* Finds a minimal unit for which a value represented in that unit has a whole part.
* For example for 0.100 Dots the unit is Millidot (100 Millidots), for 0.000075 its Microdot (75 Microdots), and so on.
* For example for 0.100 Dots the unit is Millidot (100 Millidots), for 0.000075 it's Microdot (75 Microdots), and so on.
*
* @return a minimal unit
*/
Expand All @@ -154,7 +153,7 @@ public Unit getMinimalUnit() {

/**
* Finds a minimal unit, down to specified limit, for which a value represented in that unit has a whole part.
* For example for 0.100 Dots the unit is Millidot (100 Millidots), for 0.000075 its Microdot (75 Microdots), and so on.
* For example for 0.100 Dots the unit is Millidot (100 Millidots), for 0.000075 it's Microdot (75 Microdots), and so on.
*
* @param limit the limit that stop further decrease of the unit
* @return a minimal unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
public class Units {

public static final Unit Planck = new Unit("Planck", 0);
public static final Unit Point = new Unit("Point", 3);
public static final Unit Microdot = new Unit("Microdot", "uDOT", 6);
public static final Unit Millidot = new Unit("Millidot", "mDOT", 9);
public static final Unit Dot = new Unit("Dot", "DOT", 12);
public static final Unit Microdot = new Unit("Microdot", "uDOT", 4);
public static final Unit Millidot = new Unit("Millidot", "mDOT", 7);
public static final Unit Dot = new Unit("Dot", "DOT", 10);

private final Unit[] units;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ class DotAmountFormatterSpec extends Specification {
DotAmountFormatter.autoFormatter().format(value) == exp
where:
amount | exp
5_123_456_789_000 | forLocale("5.12 Dot")
56_789_000 | forLocale("56.79 Microdot")
5_123_456_789_000 | forLocale("512.35 Dot")
23_456_789_000 | forLocale("2.35 Dot")
3_456_789_000 | forLocale("345.68 Millidot")
56_789_000 | forLocale("5.68 Millidot")
6_789_000 | forLocale("678.90 Microdot")
89_000 | forLocale("8.90 Microdot")
9_000 | forLocale("9,000.00 Planck")
}

def "Standard short"() {
Expand All @@ -73,8 +78,10 @@ class DotAmountFormatterSpec extends Specification {
DotAmountFormatter.autoShortFormatter().format(value) == exp
where:
amount | exp
5_123_456_789_000 | forLocale("5.12 DOT")
56_789_000 | forLocale("56.79 uDOT")
5_123_456_789_000 | forLocale("512.35 DOT")
56_789_000 | forLocale("5.68 mDOT")
89_000 | forLocale("8.90 uDOT")
9_000 | forLocale("9,000.00 Planck")
}

def "With group separator"() {
Expand All @@ -101,7 +108,7 @@ class DotAmountFormatterSpec extends Specification {
when:
def act = fmt.format(amount1)
then:
act == forLocale("5.12 Dot")
act == forLocale("512.35 Dot")
}

def "Converted with decimal part"() {
Expand All @@ -115,21 +122,21 @@ class DotAmountFormatterSpec extends Specification {
when:
def act = fmt.format(amount2)
then:
act == forLocale("123.46 Millidot")
act == forLocale("12,345.68 Millidot")
}

def "Converted large with decimal part"() {
setup:
def fmt = DotAmountFormatter.newBuilder()
.usingUnit(Units.Point)
.usingUnit(Units.Planck)
.fullNumber("#,##0.00")
.exactString(" ")
.fullUnit()
.build()
when:
def act = fmt.format(amount1)
then:
act == forLocale("5,123,456,789.00 Point")
act == forLocale("5,123,456,789,000.00 Planck")
}

def "Using short unit"() {
Expand All @@ -143,7 +150,7 @@ class DotAmountFormatterSpec extends Specification {
when:
def act = fmt.format(amount2)
then:
act == forLocale("123.457 mDOT")
act == forLocale("12,345.679 mDOT")
}

def "Using auto unit"() {
Expand All @@ -157,7 +164,7 @@ class DotAmountFormatterSpec extends Specification {
when:
def act = fmt.format(amount5)
then:
act == forLocale("456.79 uDOT")
act == forLocale("45.68 mDOT")
}

def "Using auto with limit unit"() {
Expand All @@ -171,7 +178,7 @@ class DotAmountFormatterSpec extends Specification {
when:
def act = fmt.format(amount8)
then:
act == forLocale("0.000789 mDOT")
act == forLocale("0.078900 mDOT")
}

def "Gives null on null input"() {
Expand Down
Loading

0 comments on commit c8e3d91

Please sign in to comment.