numbers in X.Xk back to numerical numbers Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ronkwan
Posts: 7
Joined: 04 Nov 2016, 03:41

numbers in X.Xk back to numerical numbers

18 Oct 2017, 17:24

hi all,

i used ControlGetText to grab a number but the number came in a format like 10.1k(means 10100 ?), 91.158k, 130.332k, etc

my question, is there an easy way to translate back to numerical numbers?

much thanks.
Last edited by ronkwan on 19 Oct 2017, 00:32, edited 1 time in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: numbers in X.Xk back to numberical numbers

18 Oct 2017, 17:48

Code: Select all

ControlGetText, number, ...
number := StrReplace(number,"k","00")
MsgBox % number
:?:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: numbers in X.Xk back to numberical numbers  Topic is solved

18 Oct 2017, 21:11

I've had a go at some RegEx for this, although to write '130.332k' would seem very unusual, in case you have a link or a copy of some original text.

Perhaps it's the European style of writing numbers, where '.' means ','? But then you have '10.1k' example which would contradict this.

Anyhow, I would use a script such as this with caution, in case it modifies some of the other text unintentionally.

Code: Select all

q::
vText := "10.1k 91.158k 130.332k"
vTextOrig := vText
vText := RegExReplace(vText, "\.(\d{1})k", "$100")
vText := RegExReplace(vText, "\.(\d{2})k", "$10")
vText := RegExReplace(vText, "\.(\d{3})k", "$1")
MsgBox, % vTextOrig "`r`n" vText
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: numbers in X.Xk back to numberical numbers

18 Oct 2017, 21:34

Assuming the k means 1000, I would use variable:=RegExReplace(variable,"[\.k]","") then multiply the result by 1000 (even in the same expression). You can even add in a check that your remaining number is numerical. If var is number, potentially. (Also any commas have to be separated out; also I'm considering this to be numerical...)

Here's a quick test, which adds in a comma separator.

Code: Select all

^8::
string:="91.158k"
string:=RegExReplace(string,"[\.k]","")*1000
length:=StrLen(string)
Loop, Parse, string
{
If A_Index=1
	final_answer.=A_LoopField
else If !Mod(length+1-A_Index,3)
	final_answer.="," A_LoopField
else
	final_answer.=A_LoopField
}
MsgBox % final_answer
return
Or to shorten this up with the ternary operator and || (OR) operators, with a little change in the Mod logic (dropping the ! operator), and also removing the assignment of the StrLen() to a variable (which technically makes AHK do a bit more work I believe; no noticeable impact at these short strings), and also making sure to clear the final_answer variable before you start this process (or else pressing ^8 twice in a row in this example would've resulted in 91,158,00091,158,000):

Code: Select all

^8::
string:="91.158k", final_answer.=""
string:=RegExReplace(string,"[\.k]","")*1000
Loop, Parse, string
final_answer.=(A_Index=1 || Mod(StrLen(string)+1-A_Index,3)) ? A_LoopField : "," A_LoopField
MsgBox % final_answer
return
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: numbers in X.Xk back to numberical numbers

19 Oct 2017, 00:04

Perhaps it's the European style of writing numbers, where '.' means ','? But then you have '10.1k' example which would contradict this.
Confirmed, regarding the "thousand separator" (at least for Germany).
Something that can be easily checked with changing the currency format in Excel.
10.1k = 10.100 = 10000
12.345,67 €
ronkwan
Posts: 7
Joined: 04 Nov 2016, 03:41

Re: numbers in X.Xk back to numberical numbers

19 Oct 2017, 00:10

much thanks BoBo, jeeswg and Exaskryz.

the number is extracted from a 3rd party delphi program which I have no control over with.

the number goes from 1 to around 150k on every business day. from 1 to 9999, it displays normal numerical digits without any problem until it hits 10k, 10.001k, 10.01k, 10.1k etc

the examples i provided are real original text. i am trying out your ideas and i think you have provided enough information for me to solve the problem.

thanks again.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 344 guests