How to convert Float to String with out getting E in blackberry - blackberry

Any way to convert Float to string with out getting E (exponent).
String str = String.valueOf(floatvalue);
txtbox.settext(str);
and i am using NumericTextFilter.ALLOW_DECIMAL in my textField which allow decimal but not E.
i am getting like this 1.3453E7 but i want it something like 1.34538945213 due to e i am not able to set my value in edit text.
so any way to get value with out e.

I'm not 100% sure I understand what number you're trying to format. In the US (my locale), the number 1.3453E7 is not equal to the number 1.34538945213. I thought that even in locales that used the period, or full stop (.) to group large numbers, you wouldn't have 1.34538945213. So, I'm guessing what you want here.
If you just want to show float numbers without the E, then you can use the Formatter class. It does not, however, have all the same methods on BlackBerry that you might expect on other platforms.
You can try this:
float floatValue = 1.3453E7f;
Formatter f = new Formatter();
String str = f.formatNumber(floatValue, 1);
text.setText(str);
Which will show
13453000.0
The 1 method parameter above indicates the number of decimal places to show, and can be anything from 1 to 15. It can't be zero, but if you wanted to display a number without any decimal places, I would assume you would be using an int or a long for that.
If I have misunderstood your problem, please post a little more description as to what you need.
I'll also mention this utility class that apparently can be used to do more numeric formatting on BlackBerry, although I haven't tried it myself.

Try this:
Double floatValue = 1.34538945213;
Formatter f = new Formatter();
String result = f.format("%.11f", floatValue);
Due to the floating point presentation in java, the float value 1.34538945213 has not the same representation as the double value 1.34538945213. So, if you want to get 1.34538945213 as output, you should use a double value and format it as shown in the example.

Related

Currency format is excelJS

How do you format currency in exceljs?
All I've found is this from their docs...which I have no clue how to type so I pasted it, but it doesn't seem to work
// Set Column 3 to Currency Format
ws.getColumn(3).numFmt = '�#,##0;[Red]-�#,##0';
Just took a little bit of tinkering with.
ws.getColumn(3).numFmt = '$#,##0.00;[Red]-$#,##0.00';
The #'s are optional digits. If you don't care about negative numbers being red you can leave it as $#,##0.00
For the full Accounting format, eg left-justified '$', $- for $0.00, etc, you can use:
const numFmtStr = '_("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(#_)';
cell.numFmt = numFmtStr;
see: What are .NumberFormat Options In Excel VBA?

How to set the number of digits while printing in Java?

I couldn't really clarify what I'm asking in the title. I an integer for a day and a month. I have to print the month with a 0 in front of it if it's one digit only.
For example 04 if month = 4 and so on.
This is how it's supposed to look like in C#:
Console.WriteLine("{0}.{1:00}", day, month);
Thank you.
int month = 4;
DecimalFormat formater = new DecimalFormat("00");
String month_formated = formater.format(month);
Besides the answer Fernando Lahoz provided (which is pretty specific to your case: decimal formating) you can also use System.out.format in Java which allows you to specify a format-string while printing to System.out (the format function is applicable to any PrintStream though). In your case
System.out.format("%2d %2d", day, month)
should do the trick. The %dis used for decimal integers and you can then specify any width you want just before the 'd' (2 in your case).
If you want to access the string formed for later use and not (only) print it you can use String.format. It uses the same format as System.out.format but returns the String that is formed.
A complete syntax for all formats(string, decimal, floating point, calendar, date/time, ...) can be found here.
If you'd like a quick tuto on number-formatting you can check this link or this link instead.
Good luck!

Converting type string to long

I am trying to convert the type of string to long in the following code:
PaymentReceived = String.Format(new CultureInfo("en-IN", true), "{0:n}", t.PaymentReceived),
Here t.PaymentReceived is of type long, and the PaymentReceived is of type string but I want it to be of type long.
I am using this to convert the PaymentReceived value into comma separated value.
I am trying to do as of my knowledge like
PaymentReceived = Convert.ToInt64( String.Format(new CultureInfo("en-IN", true), "{0:n}", t.PaymentReceived))
But the error is Additional information: Input string was not in a correct format.
So please help me with another solution, thank you.
The formatter n, adds additional non-numeric characters. For en-IN culture, that means a number like 1000 ends up as 1,000.00.
The Convert.ToInt64 method requires that the string be 100% numeric, including no period, which might be fine for Convert.ToDecimal, but a long is not a float. Therefore, emphatically, your string is not formatted correctly, and the error is both obvious and correct. I'm not sure what your ultimate goal here is, but it makes no sense to convert a long to a formatted string and then immediately convert it back to a long, anyways.
Assuming you have only the string and you need to format it as a long, then you need to ensure that it's formatted as a long should be. That requires:
Split on the decimal point and take just the left side:
str = str.Split(new[] { '.' })[0];
Replace any commas with empty strings:
str = str.Replace(",", "");
That assumes you know the format will something like 1,000.00. Otherwise, you may want to use a regex to replace all non-numeric characters with an empty string, instead. However, you still need to split on the decimal. Otherwise, if you just removed all non-numeric characters from something like 1,000.00, then you'd end up with 100000, a number 100 times larger than the actual string number. Also, this is all dependent on the culture. Some cultures use , as the decimal separator and . and delimiter in large numbers. If you need to handle various cultures, you'll need to adjust accordingly.

Swift, iOS: How to convert a string containing number and character (i.e ',' or ',') into number?

I have an double that i am converting using NSMassFormatter from kg to lb.
let massFormatter = NSMassFormatter()
var xyz = massFormatter.stringFromKilograms(10000.000)
// xyz "22,046.226 lb"
Now I want a way to extract the number from the string. Also if I change the Locale to say es (Spain) then the value becomes "10.000,000 kg" (It actually returns "10.000 kg", removing the decimal points for unknown reasons), but i want a way such that I can extract the number regardless of the locale. Is there any standard way? Like use a regrex or some function in NSNumberFormatter?
Thank you
There is no way to do that fully independent of locale. The main problem is that identical string will be interpreted differently depending on what locale it is run against.
Best solution will be to identify all the possible formats, define all possible formatters and try to get numberFromString: from each formatter - until the first one to obtain the correct result.
The other solution, if you're getting the data from user input, is to explain the correct format to users and provide them with instant validation - i.e. showing "incorrect format" error message. Some apps have used the UIKeyboardTypeNumberPad keyboard to restrict user, so that you'll have only numeric values.
Two keys to the problem, finding the localized units (the "kg") part in your example, and converting the string using localized grouping and decimal separators:
// convert mass to string
var lbs = massFormatter.stringFromKilograms(10000)
println("\(lbs)")
// get localized unit specifier and remove from formatted string
var units = massFormatter.unitStringFromKilograms(10000, usedUnit: nil)
if let range = lbs.rangeOfString(units) {
lbs.replaceRange(range, with: "")
}
// get number formatter and set it to use grouping separator (, or .)
let numberFormatter = NSNumberFormatter()
numberFormatter.usesGroupingSeparator = true
// get number back
var kg = numberFormatter.numberFromString(lbs)
println("\(kg)")

NSNumberFormatter returning null in Xamarin.iOS + ShinobiChart

I'm trying to get a string parsed into a NSNumber that has the % character in the end where I can use the in the ShinobiChart value property of the series.
Given the following code, I'm creating the NSNumberFormatter:
var formatter = new NSNumberFormatter ();
formatter.NumberStyle = NSNumberFormatterStyle.Percent;
var locale = new NSLocale ("en_US");
formatter.Locale = locale;
Now, I'll try to use the formatter to get the value for the chart slice label:
var value = formatter.NumberFromString (answer.RepliesShare.ToString () + "%");
This will give me the value 0.33... The point is, if I remove the "%" from the string, I'll get null on the value.
So, I need get a NSNumber that has the value of (i.e.) 30%(in other words, numberString+"%").
How can I get it?
To be more clear, I'm trying to achieve this:
Since ShinobiChart takes a Value of Type NSNumber and my RepliesShare is a decimal I'm trying just convert my decimal from 33.00 to 33%. The ShinobiChart don't take a string, otherwise I would have it handled. I found that NSNumber can be formatted with NSNumberFormatted so I thought I can use it since the Chart takes a number.
NSNumberFormatters do 2 things (amongst others):
Translate a formatted numeric NSString into an NSNumber (#"25.4%" → #"0.254")
Translate an NSNumber into a formatted NSString (#"0.254" → #"25.4%")
It's not terribly clear from your question, but I think you're trying to do the latter of these two options, but you're using the method for the first.
The following will take an NSNumber object and turn it into a %age formatted string:
var formatter = new NSNumberFormatter();
formatter.NumberStyle = NSNumberFormatterStyle.Percent;
formatter.Locale = NSLocale.CurrentLocale;
var value = formatter.stringFromNumber(new NSNumber(0.254));
This will result in value being an NSString representing 25.4%.
Provided that RepliesShare property on your answer object is an NSNumber then you can do the following to get the string you require:
var value = formatter.stringFromNumber(answers.RepliesShare);
Hope that helps.
Note: Rather than using new NSLocale ("en_US") here I used NSLocale.CurrentLocale. This will ensure that the resultant string is formatter appropriately for the current user's device. The most obvious effect that this will have with percentages is that in some European countries, a comma , is used as the decimal separator instead of a period ..
A SChartDonutSeries (and consequently SChartPieSeries) object has a LabelFormatString property. This takes a string and is used to format those labels. It has the same syntax as is used in NSNumberFormatter. Therefore, to get the behaviour you want, set the LabelFormatString after you've created the series object in your SChartDataSource subclass:
var series = new SChartDonutSeries();
series.LabelFormatString = "%0.2f%%";
This will format numbers as 2 decimal placed with a percentage sign on the end. i.e. 25.462 → 25.46%. Adjust the format string appropriately to get the format you desire. Remember that to get a % sign to appear you need to escape it - %%.
Further details on creating an appropriate format string are available here:
https://developer.apple.com/library/ios/documentation/cocoa/conceptual/Strings/Articles/formatSpecifiers.html
I think you're misunderstanding what NSNumberFormatter does. It has nothing at all to do with NSNumber objects.
NSNumberFormatter is an object for working with strings that happen to contain numeric values. It is not for working with NSNumber.
Since ShinobiChart takes a Value of Type NSNumber and my RepliesShare is a decimal I'm trying just convert my decimal from 33.00 to 33%.
You do not want to convert anything, just give 33.00 as a decimal type to ShinobiChart. I think Xamarin will convert your C# decimal to an NSNumber automatically.
If it doesn't work, try giving new NSNumber(33.00) to ShinobiChart. There should be no percents anywhere. You definitely do not want to create an NSNumberFormatter

Resources