Git Product home page Git Product logo

nim-ssfmt's Introduction

Nim Simple String Formatting

The SSFmt module is a very simple set functions for formatting numbers and strings making them suitable for printing (via echo).

Examples

The SSFmt uses the generics capability of Nim to provide a 'fmt' function. Any type can passed to fmt. The fmt function takes four parameter: the value to be "printed", the field width, the alignment, and the character to use for padding. Nim supports the "dot" notation so the first parameter (what is to be printed) can be supplied as the prefix before the dot. Named parameters, of course, are supported so the order of parameters can be changed.

import ssfmt
echo fmt(1,5,L,'.')
echo fmt(1,5,C,'.')
echo fmt(1,5,R,'.')
echo ""
echo 2.fmt(5,L,'.')
echo 2.fmt(5,C,'.')
echo 2.fmt(5,R,'.')
echo ""
echo 3.fmt(wid=5, aln=L, p='.')
echo 3.fmt(aln=L, p='.', wid=5)

SSFmt pads the supplied value to the specified width (wid parameter) with the supplied pad character (p). Default values are provided to reduce the number of parameters in the most common situations. If the wid parameter is zero (or negative) no padding will occur. If all the parameters are defaulted, the returned value is identical to the "stringifier" $.

import ssfmt
echo 4.fmt(6,L,' ')
echo 4.fmt(6,L)
echo 4.fmt(6)
echo 4.fmt()

In addition to integers strings and floats are supported. Note that the floating point version of fmtfunction uses a slightly different format for the wid field. Rather than an integer number of characters, a floating point value is used. The integer part specifies the width, the first digit of the fractional part specifies the precision (number of digits to the right of the decimal). least the first d)

import ssfmt
echo "hello".fmt(12,L,'.')
echo "hello".fmt(12,C,'.')
echo "hello".fmt(12,R,'.')
echo ""
echo 1.234567.fmt(12.2,L,'.')
echo 1.234567.fmt(12.2,C,'.')
echo 1.234567.fmt(12.2,R,'.')

On final function is provided - fmts. This function supports floating point types only and returns a representation using scientific notation. The wid parameter is again a float with the first fractional digit specifying the number of significant digits (not necessarily the number of digits after the decimal).

import ssfmt
echo 123.4567.fmts(12.2,L,'.')
echo 123.4567.fmts(12.2,C,'.')
echo 123.4567.fmts(12.2,R,'.')
echo ""
echo 1234.567e-6.fmts(12.3,L,'.')
echo 1234.567e-6.fmts(12.3,C,'.')
echo 1234.567e-6.fmts(12.3,R,'.')

One final note, the SSFmt string fmt routine supports unicode.

echo "Καλημέρα κόσμε".fmt(20,L,'.')
echo "Καλημέρα κόσμε".fmt(20,C,'.')
echo "Καλημέρα κόσμε".fmt(20,R,'.')

License

This module is released under the Beer-ware License


"THE BEER-WARE LICENSE" (Revision 42): Twist_Vector wrote this module. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return.


nim-ssfmt's People

Watchers

 avatar James Cloos 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.