Git Product home page Git Product logo

engformat-cpp's Introduction

EngFormat-Cpp

C++ based Engineering Notation Formatter

The C++ engineering formatter presented here is partly based on code found on stackoverflow [1] and partly based on the code by Jukka Korpela and David Hoerl as available from the project EngineeringNotationFormatter [2] on GitHub.

Example usage

#include "eng_format.hpp"
#include <iostream>

int main()
{
    std::cout << to_engineering_string( 1234, 3, eng_prefixed, "Pa") << std::endl;
}

Compile and run:

prompt>g++ -Wall -Wextra -I../src -o example1.exe example1.cpp ../src/eng_format.cpp && example1
1.23 kPa

Basic C++ interface

/**
 * convert a double to the specified number of digits in SI (prefix) or
 * exponential notation, optionally followed by a unit.
 */
std::string
to_engineering_string( double value, int digits, bool exponential, std::string unit = "", std::string separator = " " );

/**
 * convert the output of to_engineering_string() into a double.
 */
double
from_engineering_string( std::string text );

/**
 * step a value by the smallest possible increment.
 */
std::string
step_engineering_string( std::string text, int digits, bool exponential, bool increment );

Expanded C++ interface

extern struct eng_prefixed_t {} eng_prefixed;
extern struct eng_exponential_t {} eng_exponential;

const bool eng_increment = true;
const bool eng_decrement = false;

/**
 * convert a double to the specified number of digits in SI (prefix) notation,
 * optionally followed by a unit.
 */
std::string
to_engineering_string( double value, int digits, eng_prefixed_t, std::string unit = "", std::string separator = " " );

/**
 * convert a double to the specified number of digits in exponential notation,
 * optionally followed by a unit.
 */
std::string
to_engineering_string( double value, int digits, eng_exponential_t, std::string unit = "", std::string separator = " " );

/**
 * step a value by the smallest possible increment, using SI notation.
 */
std::string
step_engineering_string( std::string text, int digits, eng_prefixed_t, bool increment );

/**
 * step a value by the smallest possible increment, using exponential notation.
 */
std::string
step_engineering_string( std::string text, int digits, eng_exponential_t, bool increment );

Notes and References

[1] scls. Convert float number to string with engineering notation (with SI prefixe) in Python [closed]. StackOverflow.

[2] David Hoerl. EngineeringNotationFormatter. iOS Project demoing C-based Engineering Notation Formatter.

engformat-cpp's People

Contributors

joshkel avatar martinmoene avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.