1

Code:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage[english]{babel}
\usepackage{fp}
\def\x{59.3612}
\begin{document}
\noindent
x = \x\\
%what to write here?
int(x) = 59\\
frac(x) = 3612
\end{document}

What functions should I call to get 59 and 3612 out of \x?

2
  • 1
    The easiest would be to split the string at the dot. Is there a reason why you want to calculate the values? Aug 19, 2017 at 14:41
  • @UlrikeFischer "Is there a reason why you want to calculate the values?" - no. Can you suggest an answer with split?
    – user4035
    Aug 19, 2017 at 14:58

3 Answers 3

1

Note from xint package developer:

The documentation is not clear enough that \xintDecSplitR is an integer only macro (in fact it is mainly used internally, but some forgotten reason I gave it a public interface). Thus the spurious dot is an after-effect from using the macro on unexpected input. It could have been spurious ! or spurious ; if I had coded it otherwise internally. By luck the dot indeed can be removed by \xintNum.

I thus recommend rather egreg's approach, because this one works for accidental reasons. xint implemented decimal numbers at the same time as fractions and is lacking utilities which makes perfect sense for fixed point numbers only. Thus there is no high level one-shot interface to what is asked here.


Here's a way to do it with xint:

\documentclass{article}
\usepackage{xintexpr}
\def\x{59.3612}

\newcommand{\getint}[1]{\xintNum{#1}\relax}
\newcommand{\getdec}[1]{\xintNum{\xintDecSplitR{0}{#1}}\relax}

\begin{document}
    The integer part: \xintNum{\x}\relax % Or just \getint{\x}
    
    The decimal part: \xintNum{\xintDecSplitR{0}{\x}}\relax % Or just \getdec{\x}
\end{document}

enter image description here


From what I can tell from the package documentation:

\xintNum{x} truncates the fractional number x and returns an integer value.

\xintDecSplit{n}{x} cuts the number x into two pieces and returns two floats. If n is 0, then the split is performed at the decimal point. The corresponding command \xintDecSplitR{n}{x} returns the number on the right side after cutting, in this case, the decimal portion of x. I've had to wrap it in its own \xintNum to convert the float into an integer, so you don't get a spurious dot at the end.

Finally, it's possible to wrap the entire expression in your own custom commands, like I did with \getint and \getdec so it doesn't get messy in your main code.

4
  • 1
    The documentation is not clear enough that \xintDecSplitR is an integer only macro (in fact it is mainly used internally, but some forgotten reason I gave it a public interface). Thus the spurious dot is an after-effect from using the macro on unexpected input. It could have been spurious ! or spurious ; if I had coded it otherwise internally. By luck the dot indeed can be removed by \xintNum. Thanks for daring use of this macro, and next release 1.2o will improve the documentation.
    – user4686
    Aug 29, 2017 at 8:24
  • 1
    I thus recommend rather egreg's approach, because this one works for accidental reasons. xint implemented decimal numbers at the same time as fractions and is lacking utilities which makes perfect sense for fixed point numbers only. Thus there is no high level one-shot interface to what is asked here.
    – user4686
    Aug 29, 2017 at 8:37
  • @jfbu Ah no wonder the dot was appearing. Thanks a lot for the insightful explanation! I'll include a note in my answer for future visitors to refer to egreg's answer instead.
    – Troy
    Aug 29, 2017 at 9:13
  • 1
    +1 for update, I have improved the docs for next release. I almost decided to add a macro for that task, but this will wait until xint is extended with a "Decimal" module.
    – user4686
    Aug 29, 2017 at 11:45
3

Just split the input at the period.

\documentclass{article}

\makeatletter
\newcommand{\intpart}[1]{\expandafter\int@part#1..\@nil}
\def\int@part#1.#2.#3\@nil{\if\relax#1\relax0\else#1\fi}

\newcommand{\fracpart}[1]{\expandafter\frac@part#1..\@nil}
\def\frac@part#1.#2.#3\@nil{\if\relax#2\relax0\else#2\fi}
\makeatother

\begin{document}

59.3612: integral part \intpart{59.3612}, fractional part \fracpart{59.3612}

\def\x{59.3612}

\x: integral part \intpart\x, fractional part \fracpart\x

\def\x{42}

\x: integral part \intpart\x, fractional part \fracpart\x

\def\x{.3}

\x: integral part \intpart\x, fractional part \fracpart\x

\end{document}

enter image description here

0
1
\documentclass[border=1cm]{standalone}

\usepackage{fp}
\def\x{59.3612}
\begin{document}
\FPeval\front{round(\x:0)}
\FPeval\back{clip((\x-\front)*10000)}
\front\ and \back
\end{document}

enter image description here

3
  • 1
    "*10000" - is it possible to automatically determine the size of the fractional part?
    – user4035
    Aug 19, 2017 at 14:34
  • @user4035: I have no idea. Gave up! Aug 19, 2017 at 14:41
  • 1
    I think it is easier and better to use strings. Easier code and more useful for big numbers or many digits because of the way latex sees float
    – koleygr
    Aug 19, 2017 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.