I am using LiveCycle to program a calculator. I have some float calulations which I have concated into the string as below.
var n1 = Round(123/33,1)
var n2 = Round(100/25.003)
textbox1 = ConCat("between " n1 " and " n2)
n1 will display as 3.7 and n2 will display as 4.0 but with other calculations it will display as a whole number with no trailing decimals.
I want to remove the trailing zeros where applicable. I have a number of these calculations and it just seems random which ones remove trailing zeros and which ones are kept.
Help would be appreciated if there is a different, more elegant approach
Thanks
The following code should work:
var n1 = Round(123/33,1)
var n2 = Round(100/25.003)
Concat("between ", Format("zzz9.z", n1), " and ", Format("zzz9.z", n2))
While not as elegant/succinct as letting the FormCalc engine perform the string conversion for you, it should be more reliable because it's explicit.
Related
string = "6+324-909+5+55-1"
listPositive = []
listNegative = []
I would like to put numbers with "+" in front of them in listPositive and numbers with "-" in front of them in listNegative. Just the numbers without operators, but using the operators for distinction. I have managed to find a way to seperate the first number which has neither plus nor minus in front of it.
I am very very green and I will gladly hear about different ways to go about it or even suggestions for a whole different line of thinking.
I tried:
s1='678-6783742+2342+4-8'
lst=s1.split()
sample=[]
for i in lst:
if '-' in i:
sample.append(i[1:])
print(sample)
And was expecting:
['6783742', '8']
But it only worked that way when I put spaces in like this:
s1='678 -6783742 +2342 +4 -8'
In your question you already have the answer: replace- with - and + with + (note the space before operator) and then split:
s = "678-6783742+2342+4-8"
s = s.replace("+", " +").replace("-", " -")
negative = []
for i in s.split():
if i.startswith("-"):
negative.append(i[1:])
print(negative)
Prints:
['6783742', '8']
New to python, what am i doing wrong here that it wont print my total out?
second = 2
third = 3
extra = input("what is the extra value?")
total = ("first+second+thrid+extra")
print("total,")
You are printing a string by putting in double quotes
Whereas expression should be like this:
result = first + second + extra
print(result) # without quotes
I have cells that contain various information.
In these cells, there are multiple Uppercase phrases.
I would like to be able to split the contents of the cell by adding the CHAR(13) + CHAR(10) Carriage return - linefeed combination
to the start of each new Uppercase phrase.
The only consistency is that the multiple Uppercase phrases begin after a period (.) and before open parenthesis "("
Example:
- Add CRLF to start of PERSUADER
- Add CRLF to start of RIVER JEWEL
- Add CRLF to start of TAHITIAN DANCER
- Add CRLF to start of AMBLEVE
- Add CRLF to start of GINA'S HOPE
NOTE:
There are multiple periods (.) in the text.
I have highlighted the text in red for a visual purpose only (normal text/font during import).
I am OK with either formula, UDF or VBA sub.
TEXT
PERSUADER (1) won by a margin first up at Kyneton. Bit of authority about her performance there and with the stable finding form it's easy to see her going right on with that. Ran really well when placed at Caulfield second-up last prep and that rates well against these. RIVER JEWEL (2) has been racing well at big odds. I have to like the form lines that she brings back in class now. Shapes as a key danger. TAHITIAN DANCER (5) will run well. She was okay without a lot of room at Flemington last time. AMBLEVE (13) is winning and can measure up while GINA'S HOPE (11) wasn't too far from River Jewel at Flemington and ties in as a hope off that form line.
I was able to extract with this function - but not able to manipulate the data in the cell
This is my code so far:
Function UpperCaseWords(ByVal S As String) As String
Dim X As Long, Words() As String
Const OkayPunctuation As String = ",."";:'&,-?!"
For X = 1 To Len(OkayPunctuation)
S = Replace(S, Mid(OkayPunctuation, X, 1), " ")
Next
Words = Split(WorksheetFunction.Trim(S))
For X = 0 To UBound(Words)
If Words(X) Like "*[!A-Z]*" Then Words(X) = ""
Next
UpperCaseWords = Trim(Join(Words))
End Function
Your description is not the same as your examples.
None of your examples start after a dot.
Most start after a dot-space except
PERSUADER starts at the start of the string
GINA'S HOPE starts after a space
I incorporated those rules into a regular expression, but, since your upper case words can include punctuation, for brevity I just looked for
- words that excluded lower case letters and digits
- words at least three characters long
If that is not sufficient in your real data, the regex can easily be made more specific:
Option Explicit
Function upperCaseWords(S As String) As String
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.MultiLine = True
.Pattern = "^|\s(\b[^a-z0-9]+\b\s*\()"
upperCaseWords = .Replace(S, vbCrLf & "$1")
End With
End Function
as per your wording
The only consistency is that the multiple Uppercase phrases begin
after a period (.) and before open parenthesis "("
this should do:
Function UpperCaseWords(ByVal s As String) As String
Dim w As Variant
Dim s1 As String
For Each w In Split(s, ". ")
If InStr(w, "(") Then w = Chr(13) + Chr(10) & w
s1 = s1 & w
Next
UpperCaseWords = s1
End Function
Since the OP accepted the formula solution, and here is a formula answer .
Assume data put in A1
In B1, enter formula and copied across until blank :
=TRIM(RIGHT(SUBSTITUTE(TRIM(MID(SUBSTITUTE(SUBSTITUTE(" (. "&$A1," while ",". ")," (",REPT(" ",700)),COLUMN(A1)*700,700))&" ",". ",REPT(" ",300)),300))
Padding a number with leading zeros has been answered here. But in my case I have a string character followed by digits. I want to add leading zeros after the string character, but before the digits, keeping the total length to 4. For example:
A1 -> A001
A12 -> A012
A123 -> A123
I have the following code that gets me what I want, but is there a shorter way to do this without using re to split my string into text and numbers first?
import re
mystr = 'A4'
elements = re.match(r"([a-z]+)([0-9]+)", mystr, re.I)
first, second = elements.groups()
print(first + '{:0>3}'.format(second))
output = A004
You could use the following to avoid using re:
def pad_center(s):
zeros = '0' * (4 - len(s))
first, *the_rest = list(s)
return first + zeros + ''.join(the_rest)
print(pad_center('A1'))
print(pad_center('A12'))
print(pad_center('A123'))
Or, if you want to use format() you could try this:
def pad_center(s):
zeros = '0' * (4 - len(s))
first, *the_rest = list(s)
return '{}{}{}'.format(first, zeros, ''.join(the_rest))
However, I am not aware of any way to add padding to the center of a string with the format string syntax without prior processing.
as the title say, i need to affect values to several variable from one console input. I would like to store 3 number at once from an input line looking like this: -number1-space-number2-space-number3-
Right now i am doing it like this:
numbers = input("Enter three numbers separated by spaces: ")
nb1 = int(numbers.split()[0])
nb2 = int(numbers.split()[1])
nb3 = int(numbers.split()[2])
But it wouldnt surprise me if you could do something like this:
nb1, nb2, nb3 = input("Enter three numbers separeted by spaces: ",? ,?)
Replace question mark by code actually working.
So if you know a better way of doing this, i would be thankfull.
msg = "Enter three numbers separated by spaces: "
n1, n2, n3 = (int(n) for n in input(msg).split())