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>
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;
651
memset (&fmt, 0, sizeof (fmt));
653
655
decimals = gnm_fake_trunc (decimals);
654
656
if (decimals >= 128)
661
663
num = 0; /* Underflow */
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;
668
fmt.right_optional = 0;
669
fmt.right_spaces = 0;
672
fmt.decimal_separator_seen = (decimals > 0);
673
fmt.group_thousands = commas;
674
fmt.has_fraction = FALSE;
675
fmt.unicode_minus = FALSE;
677
str = g_string_new (NULL);
680
g_string_append_c (str, '-');
682
gnm_render_number (str, num, &fmt);
684
g_string_append_c (str, '0');
685
return value_new_string_nocopy (g_string_free (str, FALSE));
667
v = value_new_float (num);
669
format = g_string_sized_new (200);
671
g_string_append (format, "#,##0");
673
g_string_append_c (format, '0');
675
g_string_append_c (format, '.');
676
go_string_append_c_n (format, '0', decimals);
679
fmt = go_format_new_from_XL (format->str);
680
g_string_free (format, TRUE);
682
res = format_value (fmt, v, NULL, -1, workbook_date_conv (ei->pos->sheet->workbook));
684
go_format_unref (fmt);
687
return value_new_string_nocopy (res);
688
690
/***************************************************************************/
865
868
} else if (VALUE_IS_EMPTY (v))
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);
869
874
str = g_string_sized_new (80);
870
875
err = format_value_gstring (str, fmt, v, NULL, -1, conv);
1075
1080
static GnmValue *
1076
1081
gnumeric_dollar (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
1078
GOFormatDetails info;
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);
1086
1093
/* This is what Excel appears to do. */
1087
1094
if (decimals >= 128)
1096
1103
number = gnm_fake_round (number * p10) / p10;
1098
info = go_format_default_money ()->family_info;
1099
info.num_decimals = (int)MAX (decimals, 0);
1100
info.negative_fmt = 2;
1102
sf = go_format_new (GO_FORMAT_CURRENCY, &info);
1105
fmt_str = g_string_sized_new (150);
1107
g_string_append_c (fmt_str, '"');
1108
go_string_append_gstring (fmt_str, curr);
1109
g_string_append (fmt_str, space_sep ? "\" " : "\"");
1111
g_string_append (fmt_str, "#,##0");
1113
g_string_append_c (fmt_str, '.');
1114
go_string_append_c_n (fmt_str, '0', (int)decimals);
1117
g_string_append (fmt_str, space_sep ? " \"" : "\"");
1118
go_string_append_gstring (fmt_str, curr);
1119
g_string_append_c (fmt_str, '"');
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, ')');
1127
sf = go_format_new_from_XL (fmt_str->str);
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);
1109
/* Trim terminal space. */
1110
end = s + strlen (s);
1111
if (end != s && end[-1] == ' ')
1135
g_string_free (fmt_str, TRUE);
1114
1137
return value_new_string_nocopy (s);