Big double value Calculation yield 1e+ as Result - iPhone [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to Multiply two big double values as follows.
long double lastKnownValue;
and for eg: when i multiply two values like
1000 x 1000,i am expecting result as 1000000.
But it gives Result as 1e+06.
How can make the result as 1000000?;
for small numbers it works perfect.

The %g and %lg format strings request scientific notation for large numbers, which is what you are getting. Try using %Lf (Are you sure it's an upper case "L" for long double? I thought it would be a lower-case L.) And if you don't want any digits after the decimal separator, use %.0Lf (Actually, I'm not sure of the order of the characters for specifying the number of decimal digits in a long double. It might be %L.0f, but I think the first version is correct.)

Instead of
long double
use
unsigned long long
eg: `
unsigned long long lastKnownValue;
unsigned long long currentValue = [mainLabel.text longLongValue];
NSLog(#"%llu",lastKnownValue);
//Set the new value to the main label
mainLabel.text = [NSString stringWithFormat:#"%llu",lastKnownValue];

Related

is there a way to convert an integer to be always a 4 digit hex number using Lua

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.

Swift Iteration Character Length [duplicate]

This question already has answers here:
Generate random alphanumeric string in Swift
(24 answers)
Closed 6 years ago.
I am in the process of making one of my first apps, the aim of it is as a password generator and telling people on a scale of 1-1000 how hard it is to guess, and how hard it is to remember based on how the letters are formatted and what it looks like and how the brain remembers patterns. So far i have all the characters I want to use in an array, and I then have a for in loop that iterates through the characters, but I can't figure out how to specify the length of the password to generate, as currently it just prints each character. So, I am asking how can I make an 8 character long password generator as simply as possible, what i have so far is:
import Foundation
let chars = ["a","b","c","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u" ,"v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"]
Thank you!
var generate: String
for generate in chars {
print(generate)
}
As simply as possible use a loop and and a random number to get the character at given index:
let length = 8
var pass = ""
for _ in 0..<length {
let random = arc4random_uniform(UInt32(chars.count))
pass += chars[Int(random)]
}
print(pass)

Displaying .0 after round numbers in Swift [duplicate]

This question already has answers here:
Precision String Format Specifier In Swift
(31 answers)
Closed 8 years ago.
I’ve made a calculator using Doubles in Swift. The problem is that when I display the outcome it will display .0 at the end even if it’s a round number. I have tried the round() function but since it’s a double it still seems to always display .0 . In Objective-c i did this by typing:
[NSString stringWithFormat:#”%.0f”, RunningTotal]; //RunningTotal being the outcome
In this case there would be no decimals at all which there would if there stood #”%.3f” for example.
Does anyone know how to do this in swift? I’ve looked around on different forums but couldn’t find it anywhere... Thanks!
Your can do the same in Swift.
let runningTotal = 12.0
let string = String(format:"%.0f", runningTotal)
println(string) // Output: 12
Generally, this would round the floating point number to the next integer.
The %g format could also be used, because that does not print trailing
zeros after the decimal point, for example:
String(format:"%g", 12.0) // 12
String(format:"%g", 12.3) // 12.3
For more advanced conversions, have a look at NSNumberFormatter.

How does this regex work? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
"Rubymonk Is Pretty Brilliant".match(/ ./, 9)
How is the answer "P" calculated from this regex?
use the match method on the string
passes two arguments, a regular expression and the position in the string to begin the search.
returns the character 'P'
The criteria you posted from the Rubymonk grader answer this succinctly:
passes two arguments, a regular expression and the position in the
string to begin the search
But let's examine that in more detail. match is being passed two arguments:
/ ./, a regular expression
9, the starting position in the string
The regular expression tells us that we're looking for a space () followed by any character (.).
The starting position tells us to start at position 9 (I). So instead of applying that regex against "Rubymonk Is Pretty Brilliant", we're applying it against "Is Pretty Brilliant".
In the string "Is Pretty Brilliant", where is the first place we encounter a space followed by another character? "Is[ P]retty Brilliant", right? Thus match finds a result of P (that's space-P, matching the regex, not just P.)
To see this more clearly and to experiment further with regexes, you can try it in an irb session or in your browser using Rubular.
(Just google for RegEx + ruby, You will find explanation of regex syntax)
/ANYTHING-HERE/
Will look for ANYTHING-HERE in the text.
In Your example its (/ ./,9):
/SPACE DOT/
So it will look for space followed by single character (Dot -> single character).
9 will be "I" from the string. And that is not space, so it will go on 2 characters right. Will find space, and then will find single character "P".
That is the result.

How to convert string to UTF-8 Decimal in iOS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can you convert a string value to decimal code point in xCode? What type of encoding can I use to convert these to the correct representation?
Examples:
string: 9 in decimal code point encoding is 57
string: 1 in decimal code point encoding is 49
string: F in decimal code point encoding is 70
Can you give example to how to do this?
Get the character value instead of the string value. So instead of string #"F" you want character 'F'. NSLog(#"F = %d",'F');
You can do this with a larger string by using the method characterAtIndex:(NSUInteger) and enumerating over the string.
int encVal = (int)[someString characterAtIndex:0];
You can do this:
unichar ch = [someString characterAtIndex:0]; // assume the 1st character
int code = (int)ch;
For the string #"9", ch will be the character '9' and code will be the value 57.

Resources