예제 #1
0
void gsb_real_cunit__gsb_real_adjust_exponent ( void )
{
    GsbReal a = {1, 0};
    gint b = 4;
    GsbReal r = gsb_real_adjust_exponent ( a, b );
    CU_ASSERT_EQUAL(10000, r.mantissa);
    CU_ASSERT_EQUAL(4, r.exponent);

    a.mantissa = -11926672494897;
    a.exponent = 9;
    b = 2;
    r = gsb_real_adjust_exponent ( a, b );
    CU_ASSERT_EQUAL(-1192667, r.mantissa);
    CU_ASSERT_EQUAL(2, r.exponent);
}
예제 #2
0
파일: utils_real.c 프로젝트: jbq/grisbi
/**
 * Return the real in a formatted string with an optional currency
 * symbol, according to the locale regarding decimal separator,
 * thousands separator and positive or negative sign.
 *
 * \param number		Number to format.
 * \param currency_number 	the currency we want to adapt the number, 0 for no adaptation
 * \param show_symbol 		TRUE to add the currency symbol in the string
 *
 * \return		A newly allocated string of the number (this
 *			function will never return NULL)
 */
gchar *utils_real_get_string_with_currency ( gsb_real number,
                        gint currency_number,
                        gboolean show_symbol )
{
    struct lconv *locale = gsb_locale_get_locale ( );
    gint floating_point;

    const gchar *currency_symbol = (currency_number && show_symbol)
                                   ? gsb_data_currency_get_code_or_isocode (currency_number)
                                   : NULL;

    /* First of all if number = 0 I return 0 with the symbol of the currency if necessary */
    if (number.mantissa == 0)
    {
        if (currency_symbol && locale -> p_cs_precedes)
            return g_strdup_printf ( "%s %s", currency_symbol, "0" );
        else if (currency_symbol && ! locale -> p_cs_precedes)
            return g_strdup_printf ( "%s %s", "0", currency_symbol );
        else
            return g_strdup ("0");
    }
    else if ( (number.exponent < 0)
    || (number.exponent > EXPONENT_MAX )
    || (number.mantissa == error_real.mantissa) )
        return g_strdup ( ERROR_REAL_STRING );

    /* first we need to adapt the exponent to the currency */
    /* if the exponent of the real is not the same of the currency, need to adapt it */
    floating_point = gsb_data_currency_get_floating_point ( currency_number );
    if ( currency_number && number.exponent != floating_point )
        number = gsb_real_adjust_exponent ( number, floating_point );

    return gsb_real_raw_format_string ( number, locale, currency_symbol );
}
예제 #3
0
/**
 * retourne une chaine représentative d'un nombre avec le point comme séparateur décimal
 * et pas de separateur de milliers
 *
 * The returned string should be freed with g_free() when no longer needed.
 * */
gchar *gsb_real_safe_real_to_string ( gsb_real number, gint default_exponent )
{
    gchar buffer[G_ASCII_DTOSTR_BUF_SIZE];
    gchar format[40];
    gchar *result = NULL;
    gchar *partie_entiere;
    const gchar *sign;
    const gchar *mon_decimal_point;
    gint nbre_char;
    lldiv_t units;

    if ( (number.exponent < 0)
    || (number.exponent > EXPONENT_MAX )
    || (number.mantissa == error_real.mantissa) )
      return g_strdup ( ERROR_REAL_STRING );

    if (number.mantissa == 0)
        return g_strdup ("0.00");

    if ( default_exponent != -1 )
        number = gsb_real_adjust_exponent ( number, default_exponent );

    sign = (number.mantissa < 0) ? "-" : "";
    mon_decimal_point = ".";

    units = lldiv ( llabs (number.mantissa), gsb_real_power_10[number.exponent] );

    nbre_char = g_sprintf ( buffer, "%.0f", (gdouble) units.quot );

    partie_entiere = g_strndup ( buffer, nbre_char );

    g_snprintf ( format, sizeof ( format ), "%s%d%s",
                                            "%s%s%s%0",
                                            number.exponent,
                                            "lld" );

    result = g_strdup_printf ( format,
                            sign,
                            partie_entiere,
                            mon_decimal_point,
                            units.rem );
    g_free( partie_entiere );

/*     printf ("number.mantissa = %lld number.exponent = %d résultat = %s\n",
 *                         number.mantissa, number.exponent, result );
 */
    return result;
}