Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
Developer
Posts: 39
Registered: ‎03-09-2014
My Device: Simulator
My Carrier: NA
Accepted Solution

Converting Input Number

[ Edited ]

Hello all,

 

I have one more problem with my app. I have a texfield where user input numbers. I want to make the app automatically convert user input for example from 1000000 to 1,000,000. Can i do this with c++? I've searched across the forum, but seems no answer doing this with c++.

 

Any help would be appreciated. Thank you.

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: Converting Input Number

If you mean that you want to insert commas into the TextField as the user is typing, you could connect to the TextField's signal called "textChanging" then use QString.insert() to insert the commas where you need them and set the comma populated string into the TextField.

Developer
Posts: 263
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: Converting Input Number

It is better if the platform provides a native mechanism such as mask pattern like "#,####,####,###" to format the text input. May be it already there but I don't know.
--tom
Developer
Posts: 39
Registered: ‎03-09-2014
My Device: Simulator
My Carrier: NA

Re: Converting Input Number

Hi tommy, i've never heard about mask pattern before, could you explain more?

 

@Superdirt : I've successfully implement QString.insert() to add " , " to the number, but the app can't calculate the sum of the number if the input number use comma as the sparator (ex : 1,000,000) will be displayed as 0. Here is the full concept of the app :

 

I have TextField to insert number, then the number will be inserted to sql database. I have 2 cpp in my app, the first one is to calculate the number from the database :

 

calculate.cpp

double calculate::sum() {

	const QString pass="SELECT *FROM data";
	QSqlQuery query(pass,myDB);
	double fieldNo = query.record().indexOf("value");
	double sum=0;
	while (query.next()) {
		double values = query.value(fieldNo).toFloat();
		sum+=values;
	}

	return sum;
}

 

Then, i have cpp to convert sum to QString to be displayed in QML :

 

display.cpp

QString display::sum() {
	double total=calculate->sum();
	QString sumString = QString::number(total);
	return sumString;
}

 

If the calculation reach more than 1000000 (more than 7 digit), then the number gives error in calculation, in the end of the number it will display e+06 for example 1.22e+06. I already use double, but why did i get this error?


This is actually why i need " , " to limit the number, but if the input number use comma as sparator, the sum calculation returns to 0 (ex : 1,000,000) in the end it will be displayed as 0.

 

I hope the problem is understandable. Any help would be appreciated, thank you.

Developer
Posts: 39
Registered: ‎03-09-2014
My Device: Simulator
My Carrier: NA

Re: Converting Input Number

Problem solved, i just use math.Round(value) to solve the problem. I think this is the best way for now.