I m using nSformatter for a calculator app.
When computing, for example : 6 divided by 10 , nsformatter gives me : .6 instead of the much desired 0.6 .
For decimal numbers smaller than 1 , the 0 is omitted by NS formatter.
Of course, I wrote a couple of lines to append the resulting string with a 0 but my question is :
Is there an option in Nsformatter , to achieve the desired result ?
No more .6 but 0.6 ?
Thank you all
OK seem to have found a simple solution with adding this
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
working fine now .
Bye Bye
Related
How would you parse a date string in format "dd/MM/yyyy" using q kdb?
It is possible when the month argument is first "MM/dd/yyyy" as follows:
"D"$"1/20/2014"
2014-01-20d
However if day is first "dd/MM/yyyy"
"D"$"20/1/2014"
0Nd
KDB supports parsing of different datetime formats. Check details here:
https://code.kx.com/q/ref/casting/#tok
For your case you need to set 'z' option which specifies the format for date parsing.
0 is "mm/dd/yyyy" and 1 is "dd/mm/yyyy".
Details: https://code.kx.com/q/ref/syscmds/#z-date-parsing
This is how you do it for your example:
q) \z 1
q) "D"$"20/1/2014"
q) 2014.01.20
If you want to avoid changing system variables and have greater control over all possible date formats you can always write a custom date parser such as this:
f:{"D"$raze"0"^neg[4 2 2]$(y vs z)iasc`YYYY`MM`DD?x}
Which takes 3 parameters; date format expected, delimiter and date string. To handle your example it would be set up as follows:
q)f[`MM`DD`YYYY;"/";"1/20/2014"]
2014.01.20
It can also handle more unconventional date formats:
q)f[`MM`YYYY`DD;"p";"1p2014p20"]
2014.01.20
Obviously the above is overkill compared to inbuilt date parsing for your example but it does give a greater degree of flexibility.
Note that you do not have to pad with zeroes (tested with 3.3):
q)"." sv ("/" vs "1/20/2014") 2 0 1
"2014.1.20"
q)"D"$ "." sv ("/" vs "1/20/2014") 2 0 1
2014.01.20
In a function:
q)f:{"D"$"."sv("/"vs x)2 0 1}
q)f "1/20/2014"
2014.01.20
If you want a function that can handle both lists and individual dates:
q)g:{"D"$$[10=type x;"."sv("/"vs x)2 0 1;"."sv/:("/"vs/:x)[;2 0 1]]}
q)g "7/20/2014"
2014.07.20
q)g ("1/20/2014";"7/20/2014";"03/20/2014")
2014.01.20 2014.07.20 2014.03.20
... which is a little better than using each:
q)\ts:100000 g ("1/20/2014";"7/20/2014";"03/20/2014")
308 1168
q)\ts:100000 f each ("1/20/2014";"7/20/2014";"03/20/2014")
327 1312
... and quicker than padding/razing:
q)h:{"D"$raze"0"^neg[4 2 2]$(y vs z)iasc`YYYY`MM`DD?x}[`MM`DD`YYYY;"/";]
q)\ts:100000 h each ("1/20/2014";"7/20/2014";"03/20/2014")
615 1312
In the Julia 1.0 documentation for the Dates package it says for the Dates.dayofweek function:
"Return the day of the week as an Int64 with 1 = Monday, 2 = Tuesday, etc.."
Example from documentation after adding using Dates:
julia> using Dates
julia> Dates.dayofweek(Date("2000-01-01"))
6
Example for Monday, 9/10/2018:
julia> Dates.dayofweek(Date("2018-09-10"))
1
I do not see a way to set Sunday=1, Monday=2, etc.
Any suggestions?
EDIT: The accepted answer works great and is illustrated below in contrast to the code above:
julia> my_dayofweek(Date("2018-09-10"))
2
This functionality is hard coded. Therefore I guess that what you can do is define your own function for this:
my_dayofweek(x) = dayofweek(x + Day(1))
What I am trying is getting binary value of a number e.g
de2bi(234)
Which results me in having this answer :
0 1 0 1 0 1 1 1
now what I want is that is its reverse order without changing its values like this :
11101010
i have tried bitrevorder() function but i am not having my desired answer. any help and suggestions will be appreciated.
Example:
>>de2bi(234)
ans = 0 1 0 1 0 1 1 1
>> fliplr(ans)
ans =
1 1 1 0 1 0 1 0
Use the function fliplr. It can be used to reverse the order of array.
Try using the flag 'left-msb' (according to the documentation in http://www.mathworks.com/help/comm/ref/de2bi.html)
The commands below show how to convert a decimal integer to base three without specifying the number of columns in the output matrix. They also show how to place the most significant digit on the left instead of on the right.
t = de2bi(12,[],3) % Convert 12 to base 3.
tleft = de2bi(12,[],3,'left-msb') % Significant digit on left
The output is
t =
0 1 1
tleft =
1 1 0
You just need to use the 'left-msb' option in de2bi:
>>de2bi(234, 'left-msb')
ans =
1 1 1 0 1 0 1 0
You can use a more simple command called dec2bin which produces the desired result:
>> dec2bin(234)
ans =
11101010
Here is the docs: http://www.mathworks.com/help/matlab/ref/dec2bin.html?refresh=true
While this is an old question, I needed to do the same thing for a CRC checksum and feel I should share the results.
In my case I need to reverse 16bit numbers, so, I've tried three methods:
1) Using fliplr() to reverse as per the suggestions:
uint16(bin2dec(fliplr(dec2bin(data,16))))
To test out the speed I decided to try and checksum 12MB of data. Using the above code in my CRC, it took 2000 seconds to complete! Most of this time was performing the bit reversal.
2) I then devised a more optimal solution, though not a one line code it is optimised for speed:
reverse = uint16(0);
for i=1:16
reverse = bitor(bitshift(reverse,1), uint16(bitand(forward,1)));
forward = bitshift(forward,-1);
end
Using the same CRC code, but with this used instead of (1), it took a little over 500 seconds to complete, so already it makes the CRC calculations four times faster!
3) That is still too much time for my liking, so instead I moved everything to a mex function. This allows the use of code from the bit twiddling examples that are floating around for optimum performance. I moved the whole CRC code to the mex function and used the following two other functions to do the bit reversal.
unsigned char crcBitReverse8(unsigned char forward) {
return (unsigned char)(((forward * 0x0802LU & 0x22110LU) | (forward * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16);
}
unsigned short crcBitReverse16(unsigned short forward) {
unsigned char inByte0 = (forward & 0xFF);
unsigned char inByte1 = (forward & 0xFF00) >> 8;
return (unsigned short )((crcBitReverse8(inByte0) << 8) | (crcBitReverse8(inByte1)));
}
Just for comparison, it took just 0.14 seconds to compute the CRC for the same 12MB data chunk (and there is no mistake in the calculation, the CRC checksums for all three methods match what is expected).
So basically, if you have to do this a lot of times (e.g. for CRC) I seriously suggest you write a mex function for doing the reversal. For such a simple operation, native MATLAB code is embarrassing slow.
why not use bitget?
>> bitget( 234, 8:-1:1 )
ans =
1 1 1 0 1 0 1 0
I'm new in MATLAB, i cannot get the answer in the format that i want.
I have a basic function call, but every execution of the program gives the result in the following format :
357341279027200000/23794118819840001
It's supposed to be in decimal, for example for same execution : 15.0181.
I could not figure out why this is happening ? Can you help me, thank you !!
Type format long on the command prompt or in your script.
If that doesnt work because the value is too large, try using vpa
Note that it's just visual, internally the value computed is precise.
>d = 357341279027200000/23794118819840001
d =
15.0181
>> d * 23794118819840001 == 357341279027200000
ans =
1
>> 15.0181 * 23794118819840001 == 357341279027200000
ans =
0
Are you sure that you are not using format rat (rational). This is the reason why you may be having fractional values. If you want decimals, try format long or format long g (Long g provides the optimal length and accuracy as a decimal, up to 10 places.)
I've got a calculation for example 57 / 30 so the solution will be 1,766666667..
How do i first of all get the 1,766666667 i only get 1 or 1.00 and then how do i round the solution (to be 2)?
thanks a lot!
57/30 performs integer division. To obtain a float (or double) result you should make 1 of the operands a floating point value:
result = 57.0/30;
To round result have a look at standard floor and ceil functions.