This question already has an answer here:
Escape a dollar sign in string interpolation
(1 answer)
Closed 6 years ago.
I have just started learning scala .I want to print $ using String Interpolation
def main(args:Array[String]){
println("Enter the string")
val inputString:String=readLine()
val inputAsDouble:Double=inputString.toDouble
printf(f" You owe '${inputAsDouble}%.1f3 ")
}
Input is 2.7255 I get output as .You owe 2.73 while i want it as You owe $2.73
any pointers will be of a great help
Just double it
printf(f" You owe $$${inputAsDouble}%.1f3 ")
Result:
16/06/02 10:16:04: You owe $2,73
Related
This question already has answers here:
How do I convert a Swift Array to a String?
(25 answers)
Closed 2 years ago.
How do i Print a shuffled array without brackets and commas in swift?
For example if I create an array:
var names = ["john", "connor", "Sarra"]
print(names.shuffled())
It will print the names array with commas and brackets - I want it to be printed without anything !
You can use the joined(separator:) method:
var names = ["john", "connor", "Sarra"]
print(names.shuffled()) // ["john", "connor", "Sarra"]
print(names.shuffled().joined(separator: "")) // connorSarrajohn
print(names.shuffled().joined(separator: " ")) // connor Sarra john
Whenever I try to run this code, I get an error about str() and int():
current_year = input ("What year is it?")
current_year = int(current_year)
birth_year = input ("What year were you born in?")
birth_year = int(birth_year)
print ("You are") + (current_year - birth_year) + ("years old.")
How can I get this code to work?
Any help would be greatly appreciated!
Try casting your integers to strings with python's built-in str() method then just add the appropriate string concatenations like so:
print("You are " + str(current_year - birth_year) + " years old.")
Hopefully that helps!
Add str(number) to your print statement.
print ("You are " + str(current_year - birth_year) + " years old.")
Nick I see you are a 11 yo entrant - keep up the enthusiasm and come here to us for answers, but do your HW first.
Strings str() are basically long texts. So if you want to concatenate (join back to back) with other texts, you have to first convert the numbers into text. Hence str (1972 -1960) will give you 12 as a text string. Once it is in that form, mathematical operations on it will return an error or null, but str(current_year - birth_year) + " years old." will give you " 12 years old." - with a 'space' factored in.
In the following line of code, what is the backslash telling Swift to do?
print("The total cost of my meal is \(dictionary["pizza"]! + dictionary["ice cream"]!)")
The backslash has a few different meanings in Swift, depending on the context. In your case, it means string interpolation:
print("The total cost of my meal is \(dictionary["pizza"]! + dictionary["ice cream"]!)")
...is the same as:
print("The total cost of my meal is " + String(dictionary["pizza"]! + dictionary["ice cream"]!))
But the first form is more readable. Another example:
print("Hello \(person.firstName). You are \(person.age) years old")
Which may print something like Hello John. You are 42 years old. Much clearer than:
print("Hello " + person.firstName + ". You are " + String(person.age) + " years old")
That's called String interpolation. When you want to embed the value of a variable in a String, you have to put the variable name between parentheses and escape the opening parentheses with a backslash. This way the compiler knows that it has to substitute the value of the variable there instead of using the String literal of the variable name.
For more information on the topic, have a look at the String interpolation part of the Swift Language Guide.
This question already has answers here:
How to insert double quotes into String with interpolation in scala
(13 answers)
Closed 5 years ago.
I have the following lines in repl
scala> val accountID = "123"
accountID: String = 123
scala> s"{\"AccountID\":\$accountID\, \"ProcessMessage\":\"true\", \"Reason\":\"Integration Test Message\"}"
<console>:1: error: ';' expected but string literal found.
s"{\"AccountID\":\"$accountID\", \"ProcessMessage\":\"true\", \"Reason\":\"Integration Test Message\"}"
^
I assume it's some small silly quotations thing, but I still want to understand what I am doing wrong here. If I put the account ID directly it evaluates fine.
Use triple quotes and remove \s
scala> s"""{"AccountID":"${accountID}", "ProcessMessage":"true", "Reason":"Integration Test Message"}"""
res6: String = {"AccountID":"123", "ProcessMessage":"true", "Reason":"Integration Test Message"}
This question already has answers here:
Swift - encode URL
(19 answers)
Closed 5 years ago.
For example, this variable needs to add "25" after the "%" character. How can I do it?
var message = "cars %50 discount"
must be "cars %2550 discount"
You can do that by replacing '%' with '%25' or whatever characters you'd like to insert after '%':
var message = "cars %50 discount"
message = message.replacingOccurrences(of: "%", with: "%25")
This approach is dangerous, check the this answer for more information and for an alternative approach.
I believe this link provides the answer you need:
How can I add Variables into a String?(Swift)
For your example do:
var discountAmount = 2550
var message = "cars %\(discountAmount) discount"
(and the message prints "cars %2550 discount"