|
@@ -31,6 +31,8 @@ char conv[8] = { 0 };
|
31
|
31
|
#define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
|
32
|
32
|
#define RJDIGIT(n, f) ((n) >= (f) ? DIGIMOD(n, f) : ' ')
|
33
|
33
|
#define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
|
|
34
|
+#define INTFLOAT(V,N) (((V) * 10 * pow(10, N) + ((V) < 0 ? -5: 5)) / 10) // pow10?
|
|
35
|
+#define UINTFLOAT(V,N) INTFLOAT((V) < 0 ? -(V) : (V), N)
|
34
|
36
|
|
35
|
37
|
// Convert a full-range unsigned 8bit int to a percentage
|
36
|
38
|
const char* ui8tostr4pctrj(const uint8_t i) {
|
|
@@ -166,7 +168,7 @@ const char* i16tostr4signrj(const int16_t i) {
|
166
|
168
|
|
167
|
169
|
// Convert unsigned float to string with 1.23 format
|
168
|
170
|
const char* ftostr12ns(const float &f) {
|
169
|
|
- const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
|
|
171
|
+ const long i = UINTFLOAT(f, 2);
|
170
|
172
|
conv[3] = DIGIMOD(i, 100);
|
171
|
173
|
conv[4] = '.';
|
172
|
174
|
conv[5] = DIGIMOD(i, 10);
|
|
@@ -176,7 +178,7 @@ const char* ftostr12ns(const float &f) {
|
176
|
178
|
|
177
|
179
|
// Convert unsigned float to string with 12.3 format
|
178
|
180
|
const char* ftostr31ns(const float &f) {
|
179
|
|
- const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
|
|
181
|
+ const long i = UINTFLOAT(f, 1);
|
180
|
182
|
conv[3] = DIGIMOD(i, 100);
|
181
|
183
|
conv[4] = DIGIMOD(i, 10);
|
182
|
184
|
conv[5] = '.';
|
|
@@ -186,7 +188,7 @@ const char* ftostr31ns(const float &f) {
|
186
|
188
|
|
187
|
189
|
// Convert unsigned float to string with 123.4 format
|
188
|
190
|
const char* ftostr41ns(const float &f) {
|
189
|
|
- const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
|
|
191
|
+ const long i = UINTFLOAT(f, 1);
|
190
|
192
|
conv[2] = DIGIMOD(i, 1000);
|
191
|
193
|
conv[3] = DIGIMOD(i, 100);
|
192
|
194
|
conv[4] = DIGIMOD(i, 10);
|
|
@@ -198,7 +200,7 @@ const char* ftostr41ns(const float &f) {
|
198
|
200
|
// Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
|
199
|
201
|
const char* ftostr42_52(const float &f) {
|
200
|
202
|
if (f <= -10 || f >= 100) return ftostr52(f); // -23.45 / 123.45
|
201
|
|
- long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
|
|
203
|
+ long i = INTFLOAT(f, 2);
|
202
|
204
|
conv[2] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 1000));
|
203
|
205
|
conv[3] = DIGIMOD(i, 100);
|
204
|
206
|
conv[4] = '.';
|
|
@@ -209,7 +211,7 @@ const char* ftostr42_52(const float &f) {
|
209
|
211
|
|
210
|
212
|
// Convert signed float to fixed-length string with 023.45 / -23.45 format
|
211
|
213
|
const char* ftostr52(const float &f) {
|
212
|
|
- long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
|
|
214
|
+ long i = INTFLOAT(f, 2);
|
213
|
215
|
conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
|
214
|
216
|
conv[2] = DIGIMOD(i, 1000);
|
215
|
217
|
conv[3] = DIGIMOD(i, 100);
|
|
@@ -222,7 +224,7 @@ const char* ftostr52(const float &f) {
|
222
|
224
|
// Convert signed float to fixed-length string with 12.345 / _2.345 / -2.345 or -23.45 / 123.45 format
|
223
|
225
|
const char* ftostr53_63(const float &f) {
|
224
|
226
|
if (f <= -10 || f >= 100) return ftostr63(f); // -23.456 / 123.456
|
225
|
|
- long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
|
|
227
|
+ long i = INTFLOAT(f, 3);
|
226
|
228
|
conv[1] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 10000));
|
227
|
229
|
conv[2] = DIGIMOD(i, 1000);
|
228
|
230
|
conv[3] = '.';
|
|
@@ -234,7 +236,7 @@ const char* ftostr53_63(const float &f) {
|
234
|
236
|
|
235
|
237
|
// Convert signed float to fixed-length string with 023.456 / -23.456 format
|
236
|
238
|
const char* ftostr63(const float &f) {
|
237
|
|
- long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
|
|
239
|
+ long i = INTFLOAT(f, 3);
|
238
|
240
|
conv[0] = MINUSOR(i, DIGIMOD(i, 100000));
|
239
|
241
|
conv[1] = DIGIMOD(i, 10000);
|
240
|
242
|
conv[2] = DIGIMOD(i, 1000);
|
|
@@ -249,7 +251,7 @@ const char* ftostr63(const float &f) {
|
249
|
251
|
|
250
|
252
|
// Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
|
251
|
253
|
const char* ftostr4sign(const float &f) {
|
252
|
|
- const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
|
|
254
|
+ const int i = INTFLOAT(f, 1);
|
253
|
255
|
if (!WITHIN(i, -99, 999)) return i16tostr4signrj((int)f);
|
254
|
256
|
const bool neg = i < 0;
|
255
|
257
|
const int ii = neg ? -i : i;
|
|
@@ -262,9 +264,20 @@ const char* ftostr63(const float &f) {
|
262
|
264
|
|
263
|
265
|
#endif
|
264
|
266
|
|
|
267
|
+// Convert float to fixed-length string with +12.3 / -12.3 format
|
|
268
|
+const char* ftostr31sign(const float &f) {
|
|
269
|
+ int i = INTFLOAT(f, 1);
|
|
270
|
+ conv[2] = MINUSOR(i, '+');
|
|
271
|
+ conv[3] = DIGIMOD(i, 100);
|
|
272
|
+ conv[4] = DIGIMOD(i, 10);
|
|
273
|
+ conv[5] = '.';
|
|
274
|
+ conv[6] = DIGIMOD(i, 1);
|
|
275
|
+ return &conv[2];
|
|
276
|
+}
|
|
277
|
+
|
265
|
278
|
// Convert float to fixed-length string with +123.4 / -123.4 format
|
266
|
279
|
const char* ftostr41sign(const float &f) {
|
267
|
|
- int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
|
|
280
|
+ int i = INTFLOAT(f, 1);
|
268
|
281
|
conv[1] = MINUSOR(i, '+');
|
269
|
282
|
conv[2] = DIGIMOD(i, 1000);
|
270
|
283
|
conv[3] = DIGIMOD(i, 100);
|
|
@@ -276,7 +289,7 @@ const char* ftostr41sign(const float &f) {
|
276
|
289
|
|
277
|
290
|
// Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
|
278
|
291
|
const char* ftostr43sign(const float &f, char plus/*=' '*/) {
|
279
|
|
- long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
|
|
292
|
+ long i = INTFLOAT(f, 3);
|
280
|
293
|
conv[1] = i ? MINUSOR(i, plus) : ' ';
|
281
|
294
|
conv[2] = DIGIMOD(i, 1000);
|
282
|
295
|
conv[3] = '.';
|
|
@@ -288,7 +301,7 @@ const char* ftostr43sign(const float &f, char plus/*=' '*/) {
|
288
|
301
|
|
289
|
302
|
// Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
|
290
|
303
|
const char* ftostr54sign(const float &f, char plus/*=' '*/) {
|
291
|
|
- long i = (f * 100000 + (f < 0 ? -5: 5)) / 10;
|
|
304
|
+ long i = INTFLOAT(f, 4);
|
292
|
305
|
conv[0] = i ? MINUSOR(i, plus) : ' ';
|
293
|
306
|
conv[1] = DIGIMOD(i, 10000);
|
294
|
307
|
conv[2] = '.';
|
|
@@ -301,13 +314,13 @@ const char* ftostr54sign(const float &f, char plus/*=' '*/) {
|
301
|
314
|
|
302
|
315
|
// Convert unsigned float to rj string with 12345 format
|
303
|
316
|
const char* ftostr5rj(const float &f) {
|
304
|
|
- const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
|
|
317
|
+ const long i = UINTFLOAT(f, 0);
|
305
|
318
|
return ui16tostr5rj(i);
|
306
|
319
|
}
|
307
|
320
|
|
308
|
321
|
// Convert signed float to string with +1234.5 format
|
309
|
322
|
const char* ftostr51sign(const float &f) {
|
310
|
|
- long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
|
|
323
|
+ long i = INTFLOAT(f, 1);
|
311
|
324
|
conv[0] = MINUSOR(i, '+');
|
312
|
325
|
conv[1] = DIGIMOD(i, 10000);
|
313
|
326
|
conv[2] = DIGIMOD(i, 1000);
|
|
@@ -320,7 +333,7 @@ const char* ftostr51sign(const float &f) {
|
320
|
333
|
|
321
|
334
|
// Convert signed float to string with +123.45 format
|
322
|
335
|
const char* ftostr52sign(const float &f) {
|
323
|
|
- long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
|
|
336
|
+ long i = INTFLOAT(f, 2);
|
324
|
337
|
conv[0] = MINUSOR(i, '+');
|
325
|
338
|
conv[1] = DIGIMOD(i, 10000);
|
326
|
339
|
conv[2] = DIGIMOD(i, 1000);
|
|
@@ -333,7 +346,7 @@ const char* ftostr52sign(const float &f) {
|
333
|
346
|
|
334
|
347
|
// Convert signed float to string with +12.345 format
|
335
|
348
|
const char* ftostr53sign(const float &f) {
|
336
|
|
- long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
|
|
349
|
+ long i = INTFLOAT(f, 3);
|
337
|
350
|
conv[0] = MINUSOR(i, '+');
|
338
|
351
|
conv[1] = DIGIMOD(i, 10000);
|
339
|
352
|
conv[2] = DIGIMOD(i, 1000);
|
|
@@ -346,7 +359,7 @@ const char* ftostr53sign(const float &f) {
|
346
|
359
|
|
347
|
360
|
// Convert unsigned float to string with ____4.5, __34.5, _234.5, 1234.5 format
|
348
|
361
|
const char* ftostr51rj(const float &f) {
|
349
|
|
- const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
|
|
362
|
+ const long i = UINTFLOAT(f, 1);
|
350
|
363
|
conv[0] = ' ';
|
351
|
364
|
conv[1] = RJDIGIT(i, 10000);
|
352
|
365
|
conv[2] = RJDIGIT(i, 1000);
|
|
@@ -359,7 +372,7 @@ const char* ftostr51rj(const float &f) {
|
359
|
372
|
|
360
|
373
|
// Convert signed float to space-padded string with -_23.4_ format
|
361
|
374
|
const char* ftostr52sp(const float &f) {
|
362
|
|
- long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
|
|
375
|
+ long i = INTFLOAT(f, 2);
|
363
|
376
|
uint8_t dig;
|
364
|
377
|
conv[0] = MINUSOR(i, ' ');
|
365
|
378
|
conv[1] = RJDIGIT(i, 10000);
|