Unwanted decimal points
Greetings,
I'm trying to display a number in a text label. It's a twitter style comment box with a small text label informing the user how many characters they have left.
But I want to display the number of chars remaining as an integer, not with a decimal point! Currently I'm getting numbers like 10.0, 11.0, 12.0, etc.; but I want numbers as decimals. E.g. 10, 11, 12, etc.
Here is my code:
var comment=Titanium.UI.createTextArea({
left:10,
right:10,
top:260,
height:100,
font:{fontSize:15,fontFamily:'Helvetica Neue'},
color:'#333',
textAlign:'left'
});
scrollView.add(comment);
var remaining=Titanium.UI.createLabel({
text:"140",
top:360,
right:10,
color:'#333',
width:30,
font:{fontSize:10,fontFamily:'Helvetica Neue'}
});
scrollView.add(remaining);
var addComment=Titanium.UI.createButton({
title:'Add comment',
width:250,
top:400
});
scrollView.add(addComment);
//Event handlers:
comment.addEventListener('change',function(){
left=140-comment.value.length;
if(left>=20){
remaining.color='#333';
}else if(left<20){
remaining.color='orange';
}else if(left<5){
remaining.color='red';
}
remaining.text=parseInt(left); //<---This line prints only decimals???
});
I'm hoping someone can help.
Many thanks in advance,
(Android emulator 2.2 API on Ubuntu 10.04 x86_64, Appcelerator 1.5)
1 Answer
-
Eamonn, I had to fiddle with your code quite a bit to get it to run before I could even start looking at your number formatting problem. You really are more likely to get answers if you:
- paste code that is as simple as possible (so it just demonstrates the behaviour but with no superfluous commands)
- ensure it doesn't contain syntax errors
- ensure it works out of the box (so that someone can simply paste it directly into their app.js file and it will run successfully without any modifications).
Anyway, here is the solution. You can drop it in your app.js to see it in action:
var win = Ti.UI.createWindow({ backgroundColor:'green', navBarHidden:false }); var scrollView = Ti.UI.createScrollView({ layout:'vertical' }); win.add(scrollView); var comment=Ti.UI.createTextArea({ left:10, right:10, top:20, height:100, font:{fontSize:15,fontFamily:'Helvetica Neue'}, color:'#333', textAlign:'left' }); scrollView.add(comment); var remaining=Ti.UI.createLabel({ text:"140", top:20, right:10, color:'#333', width:30, font:{fontSize:10,fontFamily:'Helvetica Neue'} }); scrollView.add(remaining); var addComment = Ti.UI.createButton({ title:'Add comment', width:250, top:20 }); scrollView.add(addComment); comment.addEventListener('change',function(e){ var left = 140-e.source.value.length; if(left >= 20){ remaining.color = '#333'; } else if(left < 20){ remaining.color = 'orange'; } else if(left < 5){ remaining.color = 'red'; } remaining.text = left.toString(); //<---This line prints only decimals??? }); win.open();