~ubuntu-branches/ubuntu/gutsy/gnumeric/gutsy-201105201701

« back to all changes in this revision

Viewing changes to plugins/fn-string/functions.c

  • Committer: Bazaar Package Importer
  • Author(s): Gauvain Pocentek
  • Date: 2007-03-08 16:16:45 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070308161645-uotkhmwywh7dkur4
Tags: 1.7.8-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <rangefunc-strings.h>
40
40
#include <collect.h>
41
41
#include <goffice/utils/regutf8.h>
 
42
#include <goffice/utils/go-locale.h>
42
43
#include <gsf/gsf-utils.h>
43
44
#include <gsf/gsf-msole-utils.h>
44
45
#include <gnm-i18n.h>
45
46
#include <goffice/app/go-plugin.h>
 
47
#include <goffice/utils/go-glib-extras.h>
46
48
#include <gnm-plugin.h>
47
49
 
48
50
#include <limits.h>
645
647
        gnm_float num = value_get_as_float (argv[0]);
646
648
        gnm_float decimals = argv[1] ? value_get_as_float (argv[1]) : 2.0;
647
649
        gboolean commas = argv[2] ? value_get_as_checked_bool (argv[2]) : TRUE;
648
 
        GONumberFormat fmt;
649
 
        GString *str;
650
 
 
651
 
        memset (&fmt, 0, sizeof (fmt));
 
650
        GString *format;
 
651
        GOFormat *fmt;
 
652
        GnmValue *v;
 
653
        char *res;
652
654
 
653
655
        decimals = gnm_fake_trunc (decimals);
654
656
        if (decimals >= 128)
661
663
                        num = 0;  /* Underflow */
662
664
                else
663
665
                        num = gnm_fake_round (num * mult) / mult;
664
 
                fmt.right_req = fmt.right_allowed = 0;
665
 
        } else /* decimal point format */
666
 
                fmt.right_req = fmt.right_allowed = (int)decimals;
667
 
 
668
 
        fmt.right_optional         = 0;
669
 
        fmt.right_spaces           = 0;
670
 
        fmt.left_spaces            = 0;
671
 
        fmt.left_req               = 0;
672
 
        fmt.decimal_separator_seen = (decimals > 0);
673
 
        fmt.group_thousands        = commas;
674
 
        fmt.has_fraction           = FALSE;
675
 
        fmt.unicode_minus          = FALSE;
676
 
 
677
 
        str = g_string_new (NULL);
678
 
        if (num < 0.) {
679
 
                num = -num;
680
 
                g_string_append_c (str, '-');
681
 
        }
682
 
        gnm_render_number (str, num, &fmt);
683
 
        if (str->len == 0)
684
 
                g_string_append_c (str, '0');
685
 
        return value_new_string_nocopy (g_string_free (str, FALSE));
 
666
        }
 
667
        v = value_new_float (num);
 
668
 
 
669
        format = g_string_sized_new (200);
 
670
        if (commas)
 
671
                g_string_append (format, "#,##0");
 
672
        else
 
673
                g_string_append_c (format, '0');
 
674
        if (decimals > 0) {
 
675
                g_string_append_c (format, '.');
 
676
                go_string_append_c_n (format, '0', decimals);
 
677
        }
 
678
 
 
679
        fmt = go_format_new_from_XL (format->str);
 
680
        g_string_free (format, TRUE);
 
681
 
 
682
        res = format_value (fmt, v, NULL, -1, workbook_date_conv (ei->pos->sheet->workbook));
 
683
 
 
684
        go_format_unref (fmt);
 
685
        value_release (v);
 
686
 
 
687
        return value_new_string_nocopy (res);
686
688
}
687
689
 
688
690
/***************************************************************************/
856
858
                workbook_date_conv (ei->pos->sheet->workbook);
857
859
        GString *str;
858
860
        GOFormatNumberError err;
 
861
        char *lfmt;
859
862
 
860
863
        /* Why do we have to do these here?  */
861
864
        if (VALUE_IS_STRING (v)) {
865
868
        } else if (VALUE_IS_EMPTY (v))
866
869
                v = value_zero;
867
870
 
868
 
        fmt = go_format_new_from_XL (value_peek_string (argv[1]), TRUE);
 
871
        lfmt = go_format_str_delocalize (value_peek_string (argv[1]));
 
872
        fmt = go_format_new_from_XL (lfmt);
 
873
        g_free (lfmt);
869
874
        str = g_string_sized_new (80);
870
875
        err = format_value_gstring (str, fmt, v, NULL, -1, conv);
871
876
        if (err) {
1075
1080
static GnmValue *
1076
1081
gnumeric_dollar (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
1077
1082
{
1078
 
        GOFormatDetails info;
1079
1083
        GOFormat *sf;
1080
1084
        gnm_float p10;
1081
1085
        GnmValue *v;
1082
 
        char *s, *end;
 
1086
        char *s;
1083
1087
        gnm_float number = value_get_as_float (argv[0]);
1084
1088
        gnm_float decimals = argv[1] ? value_get_as_float (argv[1]) : 2.0;
 
1089
        gboolean precedes, space_sep;
 
1090
        const GString *curr = go_locale_get_currency (&precedes, &space_sep);
 
1091
        GString *fmt_str;
1085
1092
 
1086
1093
        /* This is what Excel appears to do.  */
1087
1094
        if (decimals >= 128)
1095
1102
        else
1096
1103
                number = gnm_fake_round (number * p10) / p10;
1097
1104
 
1098
 
        info = go_format_default_money ()->family_info;
1099
 
        info.num_decimals = (int)MAX (decimals, 0);
1100
 
        info.negative_fmt = 2;
1101
 
 
1102
 
        sf = go_format_new (GO_FORMAT_CURRENCY, &info);
 
1105
        fmt_str = g_string_sized_new (150);
 
1106
        if (precedes) {
 
1107
                g_string_append_c (fmt_str, '"');
 
1108
                go_string_append_gstring (fmt_str, curr);
 
1109
                g_string_append (fmt_str, space_sep ? "\" " : "\"");
 
1110
        }
 
1111
        g_string_append (fmt_str, "#,##0");
 
1112
        if (decimals > 0) {
 
1113
                g_string_append_c (fmt_str, '.');
 
1114
                go_string_append_c_n (fmt_str, '0', (int)decimals);
 
1115
        }
 
1116
        if (!precedes) {
 
1117
                g_string_append (fmt_str, space_sep ? " \"" : "\"");
 
1118
                go_string_append_gstring (fmt_str, curr);
 
1119
                g_string_append_c (fmt_str, '"');
 
1120
        }
 
1121
 
 
1122
        /* No color and no space-for-parenthesis.  */
 
1123
        g_string_append (fmt_str, ";(");
 
1124
        g_string_append_len (fmt_str, fmt_str->str, fmt_str->len - 2);
 
1125
        g_string_append_c (fmt_str, ')');
 
1126
 
 
1127
        sf = go_format_new_from_XL (fmt_str->str);
 
1128
 
1103
1129
        v = value_new_float (number);
1104
1130
        s = format_value (sf, v, NULL, -1,
1105
1131
                workbook_date_conv (ei->pos->sheet->workbook));
1106
1132
        value_release (v);
1107
1133
        go_format_unref (sf);
1108
1134
 
1109
 
        /* Trim terminal space.  */
1110
 
        end = s + strlen (s);
1111
 
        if (end != s && end[-1] == ' ')
1112
 
                end[-1] = 0;
 
1135
        g_string_free (fmt_str, TRUE);
1113
1136
 
1114
1137
        return value_new_string_nocopy (s);
1115
1138
}