Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
How to compare two Mongodb primitive type Decimal128 in go
valueA, _ := primitive.ParseDecimal128("123.00")
valueB, _ := primitive.ParseDecimal128("123.12")

how to calculate valueA < valueB?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I found an answer thanks to @Matteo

There is a function converting Decimal128 to BigInt.

and BigInt available to compare

func CompareDecimal128(d1, d2 primitive.Decimal128) (int, error) {
    b1, exp1, err := d1.BigInt()
    if err != nil {
        return 0, err
    }
    b2, exp2, err := d2.BigInt()
    if err != nil {
        return 0, err
    }

    sign := b1.Sign()
    if sign != b2.Sign() {
        if b1.Sign() > 0 {
            return 1, nil
        } else {
            return -1, nil
        }
    }

    if exp1 == exp2 {
        return b1.Cmp(b2), nil
    }

    if sign < 0 {
        if exp1 < exp2 {
            return 1, nil
        }
        return -1, nil
    } else {
        if exp1 < exp2 {
            return -1, nil
        }

        return 1, nil
    }
}

*edited for exponential part

over 4 years ago · Santiago Trujillo Report

0

Looking in the test here:

func compareDecimal128(d1, d2 primitive.Decimal128) bool {
    d1H, d1L := d1.GetBytes()
    d2H, d2L := d2.GetBytes()

    if d1H != d2H {
        return false
    }

    if d1L != d2L {
        return false
    }

    return true
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!