Wow! Thanks!
I tried out both versions. Incredible! Both of them saved me about 400 lines of code (lots of "if 0.0 then don't round, otherwise round" for multiple variables...). You guys would probably laugh if you saw my whole script, but hey, I'm still pretty new to this.
Right now I'm favoring Exaskryz' solution.
I tried
1-Log(a), but it rounded off too much (like 160 instead of 155).
The RegEx works great! Only problem is, I occassionaly have one decimal too many (like 69.04 where I was expeciting 69), but I might just have to live with that. I can't remove the
\., and I can't think of another way to limit it properly.
My goal is to round the result of
p to a maximum of one decimal place, unless that results in
0.0, in which case I want to round to the nearest non-zero number after the decimal. (I could have said that to begin with, sorry.)
Here's what I've got (just one of many in the script, but they're all the same form/principal):
Code: Select all
convertlv:
p := $1 / 28.3168467117
RegExMatch(p, "\.0*",zp)
rp := Round(p, StrLen(zp))
;rp := Round(p, 1-Log(p))
Send, {U+0020}%$1%{U+00A0}l{U+0020}^i(%rp%{U+00A0}cu{U+00A0}ft){right}{U+0020}
return
If
$1 = 1955, I get
69.04 (where my target is 69), but if
$1 = 2, I get
0.07 (which is perfect).
I tried adding
rp2 := Round(rp, 1) and changing
%rp% to
%rp2% in the Send, but that doesn't quite work either:
Code: Select all
convertlv:
p := $1 / 28.3168467117
RegExMatch(p, "\.0*",zp)
rp := Round(p, StrLen(zp))
rp2 := Round(rp, 1)
;rp := Round(p, 1-Log(p))
Send, {U+0020}%$1%{U+00A0}l{U+0020}^i(%rp2%{U+00A0}cu{U+00A0}ft){right}{U+0020}
return
By "double rounding": If
$1 = 1955, I get
69.0 (which is still okay, but not ideal), while if
$1 = 2, I get
1 (which is not accurate enough).
Again, thanks for any help!
___________
Background, in case it helps: My whole script is a hotstrings script to convert measurements as you type (see
https://autohotkey.com/boards/viewtopic.php?f=5&t=51414, which is where I started). Right now I have 14 unit/measurment conversions, plus the - / version of each one, so total of 28.
Code: Select all
hotstrings(" (\d+\.?\d*)( lv )", "convertlv")
hotstrings("(\d+\.?\d*)(\-|\/)(\d+\.?\d*)( lv )", "convertlvb")
return
convertlvb:
p := $1 / 28.3168467117
q := $3 / 28.3168467117
RegExMatch(p, "\.0*",zp)
rp := Round(p, StrLen(zp))
;rp := Round(p, 1-Log(p))
RegExMatch(q, "\.0*",zq)
rq := Round(q, StrLen(zq))
;rq := Round(q, 1-Log(q))
Send, %$1%%$2%%$3%{U+00A0}l{U+0020}^i(%rp%%$2%%rq%{U+00A0}cu{U+00A0}ft){right}{U+0020}
return
(the rest of the hotstrings code is from here:
https://autohotkey.com/boards/viewtopic.php?f=5&t=51414, and I take no credit for it; I only wish I understood how it works!)
That's why I used
zp instead of
zeroes: to catch two numbers
$1 and
$3 with another character between them (like
- or
/), e.g. 25-30 km/h or 4/5 l/100 km. There are probably much more elegant ways to do
convertlv and
convertlvb in one block, but I'm not there yet. In any case, I needed two
zeroes, so I named them
zp z(eroes)p and
zq z(eroes)q.
The
^i sets the converted measurement in
<i></i> tags in the target editor, then
{right} moves the cursor out of the italic tags so I can start typing again without the rest of my text being italic.