Vuo  2.4.0
VuoNumberFormat.c
Go to the documentation of this file.
1
10#include "type.h"
11#include "VuoNumberFormat.h"
13#include <CoreFoundation/CoreFoundation.h>
14
16#ifdef VUO_COMPILER
17VuoModuleMetadata({ "title" : "Number Format",
18 "description" : "How to convert a number into text.",
19 "keywords" : [ ],
20 "version" : "1.0.0",
21 "dependencies" : [
22 "VuoText",
23 "VuoList_VuoNumberFormat",
24 "CoreFoundation.framework"
25 ]
26 });
27#endif
29
38{
39 const char *valueAsString = "";
40 if (json_object_get_type(js) == json_type_string)
41 valueAsString = json_object_get_string(js);
42
43 VuoNumberFormat value = VuoNumberFormat_Decimal;
44
45 if (strcmp(valueAsString, "percentage") == 0)
46 value = VuoNumberFormat_Percentage;
47 else if (strcmp(valueAsString, "currency") == 0)
48 value = VuoNumberFormat_Currency;
49
50 return value;
51}
52
57{
58 char *valueAsString = "decimal";
59
60 if (value == VuoNumberFormat_Percentage)
61 valueAsString = "percentage";
62 else if (value == VuoNumberFormat_Currency)
63 valueAsString = "currency";
64
65 return json_object_new_string(valueAsString);
66}
67
73 VuoNumberFormat format,
74 VuoInteger minimumIntegerDigits,
75 VuoInteger minimumDecimalPlaces,
76 VuoInteger decimalPlaces,
77 bool showThousandSeparator)
78{
79 VuoText text = NULL;
80
81 CFNumberFormatterStyle style = kCFNumberFormatterDecimalStyle;
82
83 if (format == VuoNumberFormat_Percentage)
84 style = kCFNumberFormatterPercentStyle;
85 else if (format == VuoNumberFormat_Currency)
86 style = kCFNumberFormatterCurrencyStyle;
87
88 CFLocaleRef currentLocale = CFLocaleCopyCurrent();
89 CFNumberFormatterRef numberFormatter = CFNumberFormatterCreate(NULL, currentLocale, style);
90
91 {
92 CFNumberRef cfn = CFNumberCreate(NULL, kCFNumberIntType, &minimumIntegerDigits);
93 CFNumberFormatterSetProperty(numberFormatter, kCFNumberFormatterMinIntegerDigits, cfn);
94 CFRelease(cfn);
95 }
96
97 {
98 CFNumberRef cfn = CFNumberCreate(NULL, kCFNumberIntType, &minimumDecimalPlaces);
99 CFNumberFormatterSetProperty(numberFormatter, kCFNumberFormatterMinFractionDigits, cfn);
100 CFRelease(cfn);
101 }
102
103 {
104 CFNumberRef maxFractionDigits = CFNumberCreate(NULL, kCFNumberIntType, &decimalPlaces);
105 CFNumberFormatterSetProperty(numberFormatter, kCFNumberFormatterMaxFractionDigits, maxFractionDigits);
106 CFRelease(maxFractionDigits);
107 }
108
109 CFNumberFormatterSetProperty(numberFormatter, kCFNumberFormatterUseGroupingSeparator, showThousandSeparator ? kCFBooleanTrue : kCFBooleanFalse);
110
111 {
112 CFStringRef formattedNumberString = CFNumberFormatterCreateStringWithValue(NULL, numberFormatter, kCFNumberDoubleType, &value);
113 text = VuoText_makeFromCFString(formattedNumberString);
114 CFRelease(formattedNumberString);
115 }
116
117 CFRelease(numberFormatter);
118 CFRelease(currentLocale);
119
120 return text;
121}
122
127{
129 VuoListAppendValue_VuoNumberFormat(l, VuoNumberFormat_Decimal);
130 VuoListAppendValue_VuoNumberFormat(l, VuoNumberFormat_Percentage);
131 VuoListAppendValue_VuoNumberFormat(l, VuoNumberFormat_Currency);
132 return l;
133}
134
139{
140 char *valueAsString = "Decimal";
141
142 if (value == VuoNumberFormat_Percentage)
143 valueAsString = "Percentage";
144 else if (value == VuoNumberFormat_Currency)
145 valueAsString = "Currency";
146
147 return strdup(valueAsString);
148}
149
154{
155 return valueA == valueB;
156}
157
162{
163 return valueA < valueB;
164}
165