LTspice - limiting the number of decimals

Frans, it works beautifully when I insert the function expression itself in the node field, replacing the 'a' variable with $.

Except. When the node voltage is negative, then it displays again the three ??? ...
And you cant use abs() as there is only one function allowed.

Jan
 
Last edited:
Replace the math-block with this

;================================================= ====================================
; === FdWs Rounding functions by Frans de Wit for Signature Origin (diyAudio.com)
; === Truncate round to a fixec fixed number of figures
;================================================= ====================================
;V=sgn(v(inp,inn))*(round(abs(v(inp,inn))/(10**floor(log10(abs(v(inp,inn)))-2)))*(10**floor(log10(abs(v(inp,inn)))-2))); B-source formula for 3 figures
;V=sgn(v(inp,inn))*(round(abs(v(inp,inn))/(10**floor(log10(abs(v(inp,inn)))-5)))*(10**floor(log10(abs(v(inp,inn)))-5))); B-source formula for 6 figures
.func rndx(a,x) {sgn(a)*(round(abs(a)/(10**floor(log10(abs(a))-(x-1))))*(10**floor(log10(abs(a))-(x-1))))}; Function for x figures
.func rnd3(a) {sgn(a)*(round(abs(a)/(10**floor(log10(abs(a))-2)))*(10**floor(log10(abs(a))-2)))}; Function for 3 figures
.func rnd6(a) {sgn(a)*(round(abs(a)/(10**floor(log10(abs(a))-5)))*(10**floor(log10(abs(a))-5)))}; Function for 6 figures
;{sgn($)*(round(abs($)/(10**floor(log10(abs($))-2)))*(10**floor(log10(abs($))-2)))}; Function for 3 figures on .op data label
;{sgn($)*(round(abs($)/(10**floor(log10(abs($))-5)))*(10**floor(log10(abs($))-5)))}; Function for 6 figures on .op data label
 
About the problem reported.
LTspice will always return 1 for any negative number x in Log10(x).

Here is, again, a new set of formula's they are slightly more efficient.
;================================================= ==================================
; === FdWs Rounding functions by Frans de Wit for Signature Origin (published at diyAudio.com).
; === Round a number to a fixed number of figures, ignoring leading zero's (after the decimal separators).
;================================================= ==================================
;V=round(v(inp,inn)/(10**floor(log10(abs(v(inp,inn)))-2)))*(10**floor(log10(abs(v(inp,inn)))-2)); B-source formula for 3 figures
;V=round(v(inp,inn)/(10**floor(log10(abs(v(inp,inn)))-5)))*(10**floor(log10(abs(v(inp,inn)))-5)); B-source formula for 6 figures
.func rndx(a,x) {round(a/(10**floor(log10(abs(a))-(x-1))))*(10**floor(log10(abs(a))-(x-1)))}; Function for x figures
.func rnd3(a) {round(a/(10**floor(log10(abs(a))-2)))*(10**floor(log10(abs(a))-2))}; Function for 3 figures
.func rnd6(a) {round(a/(10**floor(log10(abs(a))-5)))*(10**floor(log10(abs(a))-5))}; Function for 6 figures
;round($/(10**floor(log10(abs($))-2)))*(10**floor(log10(abs($))-2)); Function for 3 figures on .op data label
;round($/(10**floor(log10(abs($))-5)))*(10**floor(log10(abs($))-5)); Function for 6 figures on .op data label


Check out for correctly solving this(log10(-100)).
log10(-100) - Wolfram|Alpha
 
Last edited:
I can't remember where I found this method of rounding, it seems to work:

round(I(R13)*100k)/100k
or
round(V(n006)*1k)/1k

Here a 'normalized' version of the rounding algorithm.
As you can see, it is actually the same as the one you posted, except that the divider multiplier in the formula you posted is replaced by a function rnds(...).

function rndx(<number>,<figures>); Round the value <number> to the number of digits <figures>.
{
round(
<number>
/
rnds(<number>,<figures>)
)
*
rnds(<number>,<figures>)
}

function rnds(<number>,<figures>); Compute the scaling factor needed for rounding.
(
10**floor(
log10(
abs(<number>)
)
-
<figures-1>
)
)

note: sorry: I can not find a way to display the leading spaces I typed above (to make this more readable)
 
Last edited:
Maybe I'm not understanding how this works, how are these rounding formulas need to be implemented.

Are they globally enabled on the schematic or do they need to be used on the op.data label ?

.func rndx(a,x) {round(a/(10**floor(log10(abs(a))-(x-1))))*(10**floor(log10(abs(a))-(x-1)))}; Function for x figures
.func rnd3(a) {round(a/(10**floor(log10(abs(a))-2)))*(10**floor(log10(abs(a))-2))}; Function for 3 figures
.func rnd6(a) {round(a/(10**floor(log10(abs(a))-5)))*(10**floor(log10(abs(a))-5))}; Function for 6 figures
round($/(10**floor(log10(abs($))-2)))*(10**floor(log10(abs($))-2)); Function for 3 figures on .op data label
round($/(10**floor(log10(abs($))-5)))*(10**floor(log10(abs($))-5)); Function for 6 figures on .op data label

This is so much easier - round(I(R13)*100k)/100k 🙂
 
Frans, this is excellent stuff, thank you.

Maybe I'm not understanding how this works, how are these rounding formulas need to be implemented.

OK, in the Displayed Data dialogue box, enter:

"round($/(10**floor(log10(abs($))-2)))*(10**floor(log10(abs($))-2))" for 3 significant figures (without the quotes), or:

"round($/(10**floor(log10(abs($))-5)))*(10**floor(log10(abs($))-5))" for 6 significant figures.

This is so much easier - round(I(R13)*100k)/100k 🙂

The formulae given by Frans give a result in 3 or 6 significant figures. Yours seem to be trying to give a result to 3 decimal places, which is not the same thing. Moreover, your formulae are demonstrably not very robust. Contrasting Frans's 3 s.f. formula with your formula "round($*1k)/1k" for 3 d.p.:

1.23456789V yields 1.23V (Frans) or 1.235V (you)
12.3456789V yields 12.3V (Frans) or 12.346V (you)
-98.7654321V yields -98.8V (Frans) or -98.765V (you)

However:
0.0000123456789 yields 12.3uV (Frans) or 0V (you)
12345.6789 yields 12.3KV (Frans) or 12.345679KV (you)
 
Oh my, I'm yet again having the same issue that prompted me to start this thread. Despite several years of development the smart people who've been maintaining LTspice have yet to come up with a simple way, like with a general command, to limit the number of digits for labels? C'mon folks, this isn't rocket science, make it happen!
 
I did post a message and examples on how to do that (not so easy)
Just round is not going to help it you do not know the number of digits before the decimal pont
You would like to 'round' to a number of digits independent of scale
 
Hi Frans,

Mja, je zou denken dat zoiets, het aantal decimalen zichtbaar in een schema, wel met één globale parameter in te stellen moet zijn. Helaas zijn ze bij Analog Devices nog niet zo ver, vandaar mijn frustratie.

Ik heb even, kort, gekeken naar jouw benadering maar het was mij niet duidelijk hoe/wat of waar. Dus als je wellicht een paar suggesties kunt geven?

Bedankt en groeten,

Sander.

P.s. je berichten box is vol hier op DIYaudio.
 
These are the functions

.func rndx(a,x) {round(a/(10**floor(log10(abs(a))-(x-1))))*(10**floor(log10(abs(a))-(x-1)))}; Function for x figures
.func rnd3(a) {round(a/(10**floor(log10(abs(a))-2)))*(10**floor(log10(abs(a))-2))}; Function for 3 figures
.func rnd6(a) {round(a/(10**floor(log10(abs(a))-5)))*(10**floor(log10(abs(a))-5))}; Function for 6 figures

They work by getting the mantissa of the number then dividing to a common range and after that restoring the mantissa (sort off 🙂 )
 
These are the functions

.func rndx(a,x) {round(a/(10**floor(log10(abs(a))-(x-1))))*(10**floor(log10(abs(a))-(x-1)))}; Function for x figures
.func rnd3(a) {round(a/(10**floor(log10(abs(a))-2)))*(10**floor(log10(abs(a))-2))}; Function for 3 figures
.func rnd6(a) {round(a/(10**floor(log10(abs(a))-5)))*(10**floor(log10(abs(a))-5))}; Function for 6 figures

They work by getting the mantissa of the number then dividing to a common range and after that restoring the mantissa (sort off 🙂 )

Allow me to ask a stupid question: where do I apply this? I currently have a schematic with a bunch of nodes labeled, what do I do if I want the node labeled B to be limited to 2 digits behind the comma? Do I just add the .func as a Spice directive to the schematic, or is more effort required?