Haven´t found how to convert a num to a float [closed] - kotlin

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i am trying to convert a num toFloat but i dont find anything useful at the moment,
Does anyone know how to do it?
I am using this to obtain the number in the string but i don´t find how to convert it to float
val str = "awdafafaf123asfasf"
val num = str.replace(Regex("[^0-9]"),"")
println(num)
}

You can use String.to*() category of functions to convert a string to various other data types.For example:
val str = "awdafafaf123asfasf"
val num = str.replace(Regex("[^0-9]"),"")
val intValue: Int = num.toInt()
val doubleValue: Double = num.toDouble()
val floatValue: Float = num.toFloat()
println(num)
Kotlin standard library also provides String.to*OrNull() versions of these functions which return null if conversion is not possible.

Related

Get all possible combination from a value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
how can i make a calculation to say the sum of certain 4 numbers to give a certain result like 280.
all possible combinations of numbers from 1 to 80. display all the combinations of 4 numbers that have the sum I set in the textbox. I should find out the easiest way to do this, and I would be grateful if you could help me. there should be no repetitions in a instance, like 60 60, 80 80. thus eliminating the unimportant variants, and the important ones will remain.
Textbox1.Text
68,69,70,72 = 280
67,69,70,74 = 280
66,69,70,75 = 280
65,69,70,76 = 280
64,69,70,77 = 280
and so on...
I would need a model, how could I conceive such an algorithm? Thank you very much.
Get all combinations which declares a sum from a certain number
There may be a very large number of such numbers. Therefore I'm going to first get a concise data structure that describes it, then run code that executes on every one.
Since I don't have vb.net I will do it in Python. Hopefully the translation is not too hard. I have used closures and dictionaries. I did not use the natural Pythonic iterators.
#! /usr/bin/env python3
def find_combs_data (count, target, upper):
# First we create a cache of known answers.
cached = {}
def find_combs (count_left, target_left, lower):
cache_key = (count_left, target_left, lower)
# Only do the calculation when needed.
if cache_key not in cached:
# Base case and sanity check.
if count_left == 0:
if target_left == 0:
return []
else:
return None
elif upper * count_left < target_left:
return None
elif target_left < lower * count_left:
return None
answer = {}
for i in range(lower, upper + 1):
result = find_combs(count_left - 1, target_left - i, i + 1)
if result is not None:
answer[i] = result
if len(answer):
cached[cache_key] = answer
else:
cached[cache_key] = None
return cached[cache_key]
final = find_combs(count, target, 1)
return final
def execute_on_combs (fn, count, target, upper):
passed = []
def execute_on_data (d):
if d == []:
fn(passed)
else:
for i, d_inner in d.iteritems():
passed.append(i)
execute_on_data(d_inner)
passed.pop()
execute_on_data(find_combs_data(count, target, upper))
def show (x):
print(x)
execute_on_combs(show, 4, 270, 80)

Why Float error is different in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Please see the pictures:
I'm amazing the result is different.
A CGFloat is actually a double on 64 bit platforms. (It was a float on old 32 bit platforms.)
So here you're dividing a double by a double:
CGFloat price = 88888736 / a;
^^^^^ ^^^^^^^^ ^
double int -> double double
and here you're dividing a double by a float:
CGFloat price2 = 88888736 / 100.0f;
^^^^^^ ^^^^^^^^ ^^^^^^
double int -> double float
Change 100.0f to either 100.0 or (CGFloat)100 and you should be fine.
LIVE DEMO
CGFloat is a double on your machine, therefore what you are doing is this:
double a = 100.00f
double price = 88888736 / a
float a2 = 100.00f // `float` type enforced by the trailing `f`
double price2 = 88888736 / a2
The difference is that in the first case the division is a double division while in the second case this is a float division where the result is then assigned to a double. Since float division has less precision, you get the result you are seeing.

Read primitive datatype values in Kotlin [duplicate]

This question already has answers here:
Reading console input in Kotlin
(8 answers)
Closed 5 years ago.
Note: Please do not downvote question as I tried searching but did not find anything. The question has already been marked duplicate.
How to read primitive datatype values in Kotlin?
We can use Scanner object of java but I want to implement using readLine function of kotlin.
How do I scan numbers e.g num1 and num2 and perform some operation like sum ?
How do it convert following code to koltin without using scanner?
val sc = Scanner(System.in)
val num1 = sc.nextInt()
val num2 = sc.nextInt()
val sum = sum(num1, num2)
Just do something like:
fun main(vararg args: String) {
val (a, b) = readLine()!!.split(' ')
println(a.toInt() + b.toInt())
}

Type conversion to double value in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
public class TypeConversion4 {
public static void main(String[] args) {
double d = 2D + 2d + 2. + 2l + 2L + 2f + 2F + 2.f + 2.D;
System.out.println(d); //prints 18.0
}
}
how it prints 18.0. Can anyone provide some analysis.
So, what's the problem? All these twos converted to the biggest type while summing up and then the result casted to double. But you can store 2 in long, int, double and float without any error. That means that all you have to do is to sum these 9 twos and come up with 18.0.

Percentage Plus and Minus [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Having an issue calculating money after percentages have been added and then subtracting , I know that 5353.29 + 18% = 6316.88 which I need to do in my tsql but I also need to do the reverse and take 18% from 6316.88 to get back to 5353.29 all in tsql, I might have just been looking at this too long but I just cant get the figures to calculate properly, any help please?
newVal = 5353.29(1 + .18)
origVal = newVal/(1 + .18)
6316.88 is 118%, so to get back you need to divide 6316.88 by 118, then multiply by 100.
(6316.88/118)*100=5353.29
To Add n% to X
Result = X * ( 1 + (n / 100.0))
To do the reverse
X = Result / ( 1 + (n / 100.0))