When performing calculations on large numbers, how does one properly output the result in the right String format for the end user to see? I've tried the different format specifiers for NSString and compared the results to what the iOS Calculator app shows and some of the results aren't the same. Ideally it would match the iOS Calculator results exactly.
I know it needs to be displayed in decimal format until the number is sufficiently large, and at that point it needs to be shown in scientific notation. Therefore I thought %g was the right specifier. But it doesn't match the built-in calculator for the same calculation.
[NSString stringWithFormat:#"%g", calcResult] //where calcResult is a double
Some examples:
123456.7 * 1 should display as 123456.7 but it is displayed as 123457 (puzzling!)
123456789 * 1 should display as 123456789 but it's displayed as 1.23457e+08
123456789 * 123456789 should display as 1.524158e+16 but it's displayed as 1.52416e+16
Also, how does one get the comma (or period depending on locale) to appear in the result? Are there additional specifiers one can use to get the exact format the iOS Calculator shows?
Related
I'm making a list for buying groceries in Google Sheets and have the following value in cell B4.
0.95 - Lemon Juice
2.49 - Pringle Chips
1.29 - Baby Carrots
9.50 - Chicken Kebab
What I'm trying to do is split using the dash character and combine the costs (0.95+2.49+1.29+9.50).
I've tried to use Index(SPLIT(B22,"-"), 7) and SPLIT(B22,"-") but I don't know how to use only numbers from the split string.
Does someone know how to do this? Here's a sample sheet.
Answer
The following formula should produce the result you desire:
=SUM(ARRAYFORMULA(VALUE(REGEXEXTRACT(SPLIT(B4,CHAR(10)),"(.*)-"))))
Explanation
The first thing to do is to split the entry in B4 into its component parts. This is done by using the =SPLIT function, which takes the text in B4 and returns a separate result every time it encounters a specific delimiter. In this case, that is =CHAR(10), the newline character.
Next, all non-number information needs to be removed. This is relatively easy in your sample data because the numbers always appear to the left of a dash. =REGEXEXTRACT uses a regular expression to only return the text to the left of the dash.
Before the numbers can be added together, however, they must be converted to be in a number format. The =VALUE function is used to convert each result from a text string containing a number to an actual number.
All of this is wrapped in an =ARRAYFORMULA so that =VALUE and =REGEXEXTRACT parse each returned value from =SPLIT, rather than just the first.
Finally, all results are added together using =SUM.
Functions used:
=CHAR
=SPLIT
=REGEXEXTRACT
=VALUE
=ARRAYFORMULA
=SUM
Firstly you can add , symbols start and ends of numbers with below code:
REGEXREPLACE(B4,"([0-9\.]+)",",$1,")
Then split it based of , sign.
SPLIT(A8, ",")
Try below formula (see your sheet)-
=SUM(ArrayFormula(--REGEXEXTRACT(SPLIT(B4,CHAR(10)),"-*\d*\.?\d+")))
I'm creating a Lua script which will calculate a temperature value then format this value as a 4 digit hex number which must always be 4 digits. Having the answer as a string is fine.
Previously in C I have been able to use
data_hex=string.format('%h04x', -21)
which would return ffeb
however the 'h' string formatter is not available to me in Lua
dropping the 'h' doesn't cater for negative answers i.e
data_hex=string.format('%04x', -21)
print(data_hex)
which returns ffffffeb
data_hex=string.format('%04x', 21)
print(data_hex)
which returns 0015
Is there a convenient and portable equivalent to the 'h' string formatter?
I suggest you try using a bitwise AND to truncate any leading hex digits for the value being printed.
If you have a variable temp that you are going to print then you would use something like data_hex=string.format("%04x",temp & 0xffff) which would remove the leading hex digits leaving only the least significant 4 hex digits.
I like this approach as there is less string manipulation and it is congruent with the actual data type of a signed 16 bit number. Whether reducing string manipulation is a concern would depend on the rate at which the temperature is polled.
For further information on the format function see The String Library article.
I'm building a calculator like. Every time I press one of the number buttons of the calculator, I would like to have the label displaying the input, to concatenate the new input to the display.
Let's say I press 1 then 2 then 5 and the label displays 125. So far I was converting each number into a string and was appending them one after one. But I would like to make it "localized". So that if the locale is US, for 1,256.43 it display 1,256.43. If the locale is FR, it displays 1 256,43.
For that I was doing the same than before but to convert the number to string I was using a NSNumberFormatter. The problem is when I get the string 1 256 and I convert it into double (to then use the formatter) with NSString(string: "1 256").doubleValue I get 1 and not 1 256
What do I do wrong?
I have some forms on my site where after a user enters currency values those inputs are put into a calculator.
The input needs to be either floats or integers but I want it so that if they enter '$200,000' or '200000' it will both result in '200000.0' after clicking submit. (sort of the opposite of number_to_currency)
I was thinking something like
text = text.gsub(/[$,]/, '').to_f
or better yet take out all non digit characters but this will result in an unintentional zero in the output.
I also would like to format the result in the view so that if the resulting float has nothing after the decimal eg 200.00 then it will round up to 200. Otherwise it will round to 2 decimal place
Most preferably I would like it to work like the presence validator before the form actually submits.
What's the best way to go about implementing this? I'm thinking there is something obvious I've missed.
For the second part, you could do this:
# after converting text to a float
text.modulo(1) == 0 ? text.to_i : sprintf("%.2f", text)
For example, if text = 200000.0, we get 200000 using the above.
On the other hand, if text = 200000.1, we get 200000.10 using the above.
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.