Skip to content

Instantly share code, notes, and snippets.

@JosephTLyons
Last active July 10, 2020 00:50
Show Gist options
  • Save JosephTLyons/5653f7f6a57c154fc68f68278245dd6c to your computer and use it in GitHub Desktop.
Save JosephTLyons/5653f7f6a57c154fc68f68278245dd6c to your computer and use it in GitHub Desktop.
Another floatToString snippet, this time, relying on CHOC (link in the code)
//
// main.cpp
// CHOC_test
//
// Created by Joseph Lyons on 7/9/20.
// Copyright © 2020 Joseph Lyons. All rights reserved.
//
// Find the choc_FloatToString.h at:
// https://github.com/julianstorer/choc
#include <iostream>
#include "choc_FloatToString.h"
std::string floatToTruncatedString(const double number, const unsigned int precision)
{
std::string doubleString = choc::text::floatToString(number);
long int decimalPos = doubleString.find('.');
if (decimalPos != -1)
doubleString = doubleString.substr(0, decimalPos + 1 + precision);
return doubleString;
}
int main(int argc, const char * argv[])
{
std::cout << floatToTruncatedString(234.236346435, 2) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment