Put commas in a number string? - gwt

Given a number, I'd like to transform it into a string, but insert commas in the thousands place etc, like:
int number = 123456;
String formatted = String.valueOf(number);
println(formatted); // but print "123,456"?
does GWT offer a way of doing this, or should we write our own method?
Thanks

The first one is to format a number with decimal points and include a comma. The other is with out decimal points. I'm giving this out because it wasn't so easy for me to figure it out the first time when I was starting out.
private NumberFormat decFormat = NumberFormat.getFormat("#,##0.00;(#,##0.00)");
private NumberFormat intFormat = NumberFormat.getFormat("#,##0;(#,##0)");

Use NumberFormat provided by GWT.

Related

Expected '33990Times Jobs' to be greater than 0

My code:
var listItemText = element.all(by.css(".list-group-item.ng-binding.ngscope")).get(0).getText()
.then(function(text){ return text.replace(/[\r\n]/g, "")
})
expect(listItemText).toBeGreaterThan(0);
How to compare if string is number with texts should be greater than 0 in protractor.
If you want to convert String to a Number you can either use parseFlaot for floating point numbers or parseInt for integers. As long as it will not start with letter characters it will cut off the non number part. Also watch out with parsing the number as it will also cut of leading 0s in front of a number - you might want to improve your regexp to grab only the number from where you expect it to be in the string to make it more bulletproof.
Also you don't need to use element.all(locator).get(0), element(locator) for multiple occurrences will always return first element found.
element(by.css(".list-group-item.ng-binding.ngscope")).getText()
.then(function(text){
var listItemText = text.replace(/[\r\n]/g, "");
expect(praseFloat(listItemText)).toBeGreaterThan(0);
});
Expected '33990Times Jobs' to be greater than 0
First of all, you are comparing a string with a number. And, the string itself contains the extra Times Jobs part. Let's extract all the digits from the text and use parseInt to convert a string to an integer:
var listItemText = element.all(by.css(".list-group-item.ng-binding.ngscope")).first().getText().then(function(text) {
return parseInt(text.match(/\d+/)[0]);
});
expect(listItemText).toBeGreaterThan(0);

How to make a simple division of Double by Int in Swift?

Looking at various posts on this topic but still no luck. Is there a simple way to make division/conversion when dividing Double (or Float) with Int? Here is a simple example in playground returning and error "Double is not convertible to UInt8".
var score:Double = 3.00
var length:Int = 2 // it is taken from some an array lenght and does not return decimal or float
var result:Double = (score / length )
Cast the int to double with var result:Double=(score/Double(length))
What this will do is before computing the division it will create a new Double variable with int inside parentheses hence constructor like syntax.
You cannot combine or use different variable types together.
You need to convert them all to the same type, to be able to divide them together.
The easiest way I see to make that happen, would be to make the Int a Double.
You can do that quite simply do that by adding a ".0" on the end of the Integer you want to convert.
Also, FYI:
Floats are pretty rarely used, so unless you're using them for something specific, its also just more fluid to use more common variables.

AchartEngine timeseries double value to date

I can add the date/time value for the X axis to TimeSeries and if I use getPadding(), it returns a double so how can I convert this double to date again ?
Example returned double value: 1.40669949E12 (Also I tried to convert from string but not worked.)
now I want to convert this value to formatted date, Is it possible ?
The value you are getting needs to be rounded to a long using Math.round() and then using this long you can build a Date that you can format any way you need.
String format = "h:mma"; // for example
Double d = 1.40669949E12;
String formattedStr = new SimpleDateFormat(format).format(new Date(Math.round(d)));

formatting NSDecimalNumber issue

I'm trying to create NSDecimalNumber with simply format like: 123.00 with two fractional digits after dot, always. I tried use the NSFormatter and many other ways like converting float from string and creating then NSDecimalNumber from this string, but it's not working.
The problem is that I need only NSDecimalNumber in this format, not NSString or any other.
Thanks for any advice,
Paul
You may get idea from this.
float num = 123.1254546;
NSString *str = [NSString stringWithFormat:#"%.2f",num];
NSLog(#"%.2f %#",num,str);
I think you simply need to do Type Casting operation for Two times as bellow
float value = 123.562;
int myDigit = value;
it gives value 123 in myDigit variable
NSLog(#"MyDigit in Decimal = %d",myDigit);
Output is MyDigit in Decimal = 123
now if you want output like 123.000 then simply write as bellow
float valueWithZiro = myDigit;
NSLog(#"MyDigit with 000 == %3f",valueWithZiro);
Output is MyDigit in Decimal = 123.000
NSDecimalNumber, like NSNumber, cannot contain formatting information. The object structure simply doesn't support it. It represents a number, not the way the number is displayed.
You can convert it to a formatted NSString (which you say you don't want). But you can't do what you're asking.
You convert it to a formatted NSString using an NSNumberFormatter. It's the object that allows you to specify the decimal and thousands separators, number of decimal places to display, the currency symbol, etc.
Maybe you were looking to store a NSNumberDecimal with just two digits after the fraction?
If so NSDecimalNumberBehaviors is your friend.
I had a similar need and used the following:
self.decimalHandlingBehaviorForApp = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundUp
scale:2 raiseOnExactness:NO
raiseOnOverflow:NO raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
Edit: added example of using it
// update the taxable total first
self.cartTaxableTotal = [self.cartTaxableTotal decimalNumberByAdding:itemAdded.priceOfItem
withBehavior:self.decimalHandlingBehaviorForApp];

NSDecimalNumber and popping digits off of the end

I'm not quite sure what to call it, but I have a text field to hold a currency value, so I'm storing that as a NSDecimalNumber. I don't want to use the numbers & symbols keyboard so I'm using a number pad, and inferring the location of a decimal place like ATMs do. It works fine for entering numbers. Type 1234 and it displays $12.34 but now I need to implement back space. So assuming $12.34 is entered hitting back space would show $1.23. I'm not quite sure how to do this with a decimal number. With an int you would just divide by 10 to remove the right most digit, but that obviously doesn't work here. I could do it by some messy converting to int / 10 then back to decimal but that just sounds horrific... Any suggestions?
Call - (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(id < NSDecimalNumberBehaviors >)behavior on it
How about using stringValue?
1) NSDecimalNumber to String
2) substring last
3) String to NSDecimalNumber
Below is an example for Swift 3
func popLastNumber(of number: NSDecimalNumber) -> NSDecimalNumber {
let stringFromNumber = number.stringValue //NSNumber property
let lastIndex = stringFromNumber.endIndex
let targetIndex = stringFromNumber.index(before: lastIndex)
let removed = stringFromNumber.substring(to: targetIndex)
return NSDecimalNumber(string: removed)
}
If your input number is a single digit, it would return NaN.
You could replace it to NSDecimalNumber.zero if you need.
It may works like delete button on calcultor.
It's not tested much.
If someone found another NaN case, please report by reply.