Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda