This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfixed8.c
45 lines (42 loc) · 1.7 KB
/
fixed8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*******************************************************************************
* (c) 2019 All BNB Chain Developers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include "fixed8.h"
#include <string.h>
#define DECIMAL_SCALE 8
#define ZERO_FRACTION "00000000"
int fixed8_str_conv(char *output, char *input, char terminator) {
size_t input_len = strlen(input);
if (strrchr(output, '.')) return 0; // already converted
char tmp[DECIMAL_SCALE + 1];
tmp[DECIMAL_SCALE] = '\0'; // just in case
if (input_len <= DECIMAL_SCALE) { // satoshi amount
strcpy(tmp, input);
output[0] = '0';
output[1] = '.';
strcpy(&output[2], ZERO_FRACTION);
int add_decs = DECIMAL_SCALE - strlen(tmp);
strcpy(&output[2 + add_decs], tmp);
output[input_len + 2 + add_decs] = terminator;
return 1;
}
int input_dec_offset = input_len - DECIMAL_SCALE;
strcpy(tmp, &input[input_dec_offset]);
output[input_dec_offset] = '.';
strncpy(output, input, input_len - DECIMAL_SCALE);
strcpy(&output[input_dec_offset + 1], tmp);
output[input_len + 1] = terminator;
return 1;
}