Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Created November 30, 2024 01:43
Show Gist options
  • Save Ratstail91/80958f19f18ee743a6c7c60a4fae9e72 to your computer and use it in GitHub Desktop.
Save Ratstail91/80958f19f18ee743a6c7c60a4fae9e72 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
char* stripZeroes(char* buffer) {
unsigned int length = strlen(buffer);
//find the decimal, if it exists
unsigned int decimal = 0;
while (decimal != length && buffer[decimal] != '.') decimal++;
//wipe the trailing zeros
while(decimal != length && buffer[length-1] == '0') buffer[--length] = '\0';
return buffer;
}
void check(char* arg) {
char buffer[256];
strncpy(buffer, arg, 256);
char* result = stripZeroes(buffer);
printf("%s : %s\n", arg, result);
}
int main(int arc, char* argv[]) {
check("123");
check("1230");
check("123.45");
check("123.4500");
check("123.004500");
check("123.0045");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment