Skip to content

Instantly share code, notes, and snippets.

@mikkelee mikkelee/dms.tex Secret
Created May 25, 2019

Embed
What would you like to do?
\documentclass{article}
\usepackage{lipsum}
\usepackage{xparse}
\usepackage{pgfmath}
% defaults:
\newcommand*\skillingfactor{16}
\newcommand*\markfactor{6}
\newcommand\moneyadd[2]{%
\toskilling{#1}%
\edef\tmp{\skilling}%
\toskilling{#2}%
\pgfmathparse{\tmp + \skilling}%
\todms{\pgfmathresult}%
}
\newcommand\todms[1]{%
\pgfmathsetmacro\daler{div(#1, \skillingfactor * \markfactor)}\daler%
.%
\pgfmathsetmacro\mark{div(#1 - \skillingfactor * \markfactor * \daler, \skillingfactor)}\mark%
.%
\pgfmathsetmacro\skilling{int(#1 - \skillingfactor * \markfactor * \daler - \skillingfactor * \mark)}\skilling%
}
\makeatletter
\newcommand\dkm@toskilling[3]{%
\pgfmathsetmacro\skilling{int(#1 * \markfactor * \skillingfactor + #2 * \skillingfactor + #3)}%
}
\NewDocumentCommand\toskilling{ > {\SplitArgument { 2 } { . } } m }{%
\dkm@toskilling#1%
}
\makeatother
\begin{document}
With default markfactor 6:
\begingroup
\toskilling{1.2.3} \skilling{} = 1*6*16 + 2*16 + 3 = 96 + 32 + 3 = 131
\toskilling{3.2.1} \skilling{} = 3*6*16 + 2*16 + 1 = 288 + 32 + 1 = 321
\todms{400} = 4*6*16 + 1*16 + 0 = 384 + 16 = 4.1.0
\todms{452} = \moneyadd{1.2.3}{3.2.1} = 452 = 384 + 64 + 2 = 4*6*16 + 4*16 + 2 = 4.4.4
\endgroup
Resetting markfactor to 4:
\begingroup
\renewcommand*\markfactor{4}
\toskilling{2.0.0} \skilling{} = 2*4*16 + 0*16 + 0 = 128
\toskilling{0.1.2} \skilling{} = 0*4*16 + 1*16 + 2 = 18
\todms{241} = 3*4*16 + 3*16 + 1 = 192 + 48 + 1 = 241
\todms{146} = \moneyadd{2.0.0}{0.1.2} = 146 = 128 + 16 + 2 = 2*4*16 + 1*16 + 2 = 2.1.2
\endgroup
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.