I have a textField value as 12345678955. I want to format this value as 1,234,567.8955
Want to seperate the value with comma.
I have tired with some codes. But it doesn't work.
Well, you would want to get your 4 decimal places you would need to divide your number by 10000:
var newNumber = parseInt($.yourTextField.value);
newNumber = Math.round(Number(newNumber)) / 10000;
console.log(newNumber); // 1234567.8955
Next you want to add your commmas:
var parts = newNumber.toString().split(".");
var num = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");
console.log(num); // 1,234,567.8955
Thats the functionality, how you tie that to your textField and by which event listener, is yours to work out.
(answer adapted from https://stackoverflow.com/a/25127753/829989 which you could have easily found on your own)
Related
So here is my Kotlin Code:
val intBoth = 1_391_000_000 + 1_432_342_859
println("China plus India: " + intBoth)
And the result is negative:
China plus India: -1471624437
What went wrong here?
If you you don't select a your variable type (like Long,Float,Bytes,Shorts), the kotlin asumes it is an Integer.
The variable types have boundary conditions for representing number values.
The boundary of Integers are between -2,147,483,648 and 2,147,483,647 (It's about byte of size). The variables of your calculation of summiation are Integer so the complier expect the total value is Integer, but the result is out of the range of Integer variable type. Yo can overcome the problem by convert to Long your types of elements. Write .toLong end of the variables for converting
fun main() {
val intBoth = 1391000000.toLong() + 1432342859.toLong()
println("China plus India: " + intBoth)
}
Also you can use L instead of .toLong().
val intBoth = 1391000000L + 1432342859L
I'm pretty new to kotlin and would like to know how to format a number with commas.
Currently, my textview shows a number without any commas, i.e, 15000.
I want it to show 15,000 instead.
Here's my code that I want to format:
txtTotalActive.text = it.statewise[0].active
"it.statewise[0].active" is an object that shows number but as I said, it shows without any commas.
Solution:
var inoutValue = it.statewise[0].active
val number = java.lang.Double.valueOf(inoutValue)
val dec = DecimalFormat("#,###,###")
val finalOutput = dec.format(number)
txtTotalActive.text = finalOutput
"%,.2f".format(text.toFloat()) <,> = grouping , 2 = precision/number of decimals after "."
I have a "for-loop" in Kotlin which is going to run my code 6 times.
I also have a textView on the app and want to see these 6 results shown there.
I can easily println() the results.
However, If I set the text of textView to these results, it only gets the last result.
What I like to do printing out all 5 results in textView (suggestedNums ) as each result is a separate line.
Is it even possible?
Any help appreciated.
Thanks.
for (i in 1..6) {
val s: MutableSet<Int> = mutableSetOf()
//create 5 numbers from numbers
while (s.size < 5) {
val rnd = (numbers).random()
s.add(rnd)
}
// remove all 5 random numbers from numbers list.
numbers.removeAll(s)
// sort 5 random numbers and println
println(s.sorted())
// set suggestedNums text to "s"
suggestedNums.text = s.sorted().toString()
}
You can do it in 2 ways
replace
suggestedNums.text = s.sorted().toString()
with
suggestedNums.text = suggestedNums.text.toString() + "\n" + s.sorted().toString()
Create a string and append the results with "\n" and set the text outside the for loop
Example :
value = 0.0
I want to covert it to
Ans :value = 00.0
example
value = 12.0
Ans: 12.0
example
value = 1.0
Ans: 01.0
Right now for float values i am using String.format( " %.2f" ,variable_value)
is there any way like this to convert values
Thanks
you can use string.format method like below:
String value= "_%02d" + "_%s";
it converts numbers like :
String.format("%02d", 1); // => "01"
you can see forrmatter doc for other modifiers
I am trying to add a number and a text input value to display in a label. here is my code thus far.
'lblAnswer.text = bloodglucose + 100;'
Please tell me what I am doing wrong.
Please try following answer -
bloodglucose += 100;
lblAnswer.text = String(bloodglucose);
Hope this will work :)
Sunil is correct - when doing mixed type addition, the UI input first needs to be coerced to either int or Number. IE: Number(bloodglucose) + 100; This assumes bloodglucose is actually a getter to the input text reference. If it's not, then you need to coerce the property and not the id of the component.
Getter: public function get bloodglucose():Number { return Number(myInput.text); }
In method: lblAnswer.text = bloodglucose + 100;
or (bloodglucose is a UIComponent):
In method: lblAnswer.text = Number(bloodglucose.text) + 100;
You should use String(int i)
lblAnswer.text = String(bloodglucose + 100);
Update: What about something like this:
var i:int = bloodglucose + 100;
var s:String = String(i);
lblAnswer.text = s;
** Update ,
I am changing the code from the update that was previously posted. I initially found that because I was including the string value inside of the equation this is what was prompting an error. You have to wrap the converted components to Number inside of the string all together. Basically convert the components to a number, then convert the answer received into a string.
Below is an example of the wrong code.
txtAnswer = (String(Number(bloodglucose)+100)) / 36)).toFixed(2)
Below this line is the fixed code.
txtAnswer.text = String( (Number(bloodglucose.text) + (Number(100))/ (Number(36))).toFixed(2) ;
The .toFixed Property signifies how many decimal places I want the returned value to display.