Bitwise "and" operation into the kotlin - kotlin

If negative number is from -31 to -1 then I would like represent it into the format 111XXXXX.
I try to do it using "and" bitwise operator:
println("0b00011111 & 0xe0 is ${0b00011111 and 0xe0}")
println("31 & 0xe0 is ${31 and 0xe0}")
println("0b00011111 & 0b11100000 is ${0b00011111 and 0b11100000}")
But the result is always 0. Where did I make the mistake?

It prints 0 because 00011111 and 11100000 always returns 0. The return type of and is Int, so if you want to print it in base 2 with leading zeroes, you have to format it.
To convert it to a String in base 2, you can call the toString method on Int with a radix parameter:
val numberString = (0b00011111 and 0b11100000).toString(2);
This will give you the number in binary format, but without leading zeroes. You need to left-pad with zeroes to get the format you want. I leave that task up to you (hint: padStart) ;)

Related

Coverting Float to String with comma without Rounding

I'm using this for adding commas into number.
val commaNumber = NumberFormat.getNumberInstance(Locale.US).format(floatValue)
floatValue is 8.1E-7 , but commaNumber shows just 0.
How to covert Float to String with comma without Rounding?
For the US locale, the maximumFractionalDigits of the number format is 3, therefore, it will try to format 8.1e-7 with only 3 fractional digits, which makes it 0.000. Since the minimumFractionalDigits is also 0, it tries to remove all the unnecessary 0s, making the final result "0".
You should set maximumFractionalDigits to at least 7 if you want to precisely display the number 8.1e-7.
val numberFormat = NumberFormat.getNumberInstance(Locale.US)
numberFormat.maximumFractionDigits = 7
val commaNumber = numberFormat.format(floatValue)
As for the commas, that is already part of the US locale. It uses grouping, and , is its grouping separator. The commas will be inserted if they are needed.

FormatNumber replacing number with 0

Not understanding this:
Number returned from DataReader: 185549633.66000035
We have a requirement to maintain the number of decimal places per a User Choice.
For example: maintain 7 places.
We are using:
FormatNumber(dr.Item("Field"), 7, TriState.false, , TriState.True)
The result is: 185,549,633.6600000.
We would like to maintain the 3 (or 35) at the end.
When subtracting two numbers from the resulting query we are getting a delta but trying to show these two numbers out to 6,7,8 digits is not working thus indicating a false delta to the user.
Any advice would be appreciated.
Based on my testing, you must be working with Double values rather than Decimal. Not surprisingly, the solution to your problem can be found in the documentation.
For a start, you should not be using FormatNumber. We're not in VB6 anymore ToTo. To format a number in VB.NET, call ToString on that number. I tested this:
Dim dbl = 185549633.66000035R
Dim dec = 185549633.66000035D
Dim dblString = dbl.ToString("n7")
Dim decString = dec.ToString("n7")
Console.WriteLine(dblString)
Console.WriteLine(decString)
and I saw the behaviour you describe, i.e. the output was:
185,549,633.6600000
185,549,633.6600004
I read the documentation for the Double.ToString method (note that FormatNumber would be calling ToString internally) and this is what it says:
By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.
I then tested this:
Dim dbl = 185549633.66000035R
Dim dblString16 = dbl.ToString("G16")
Dim dblString17 = dbl.ToString("G17")
Console.WriteLine(dblString16)
Console.WriteLine(dblString17)
and the result was:
185549633.6600004
185549633.66000035

How to format integer as string with 2 digits?

I would like to format an integer 9 to "09" and 25 to "25".
How can this be done?
You can use either of these options:
The "0" Custom Specifier
value.ToString("00")
String.Format("{0:00}", value)
The Decimal ("D") Standard Format Specifier
value.ToString("D2")
String.Format("{0:D2}", value)
For more information:
Custom Numeric Format Strings
Standard Numeric Format Strings
If its just leading zero's that you want, you can use this:
value.tostring.padleft("0",2)
value.ToString().PadLeft(2, '0'); // C#
If you have 2 digits, say 25 for example, you will get "25" back....if you have just one digit, say 9 for example, you will get "09"....It is worth noting that this gives you a string back, and not an integer, so you may need to cast this later on in your code.
String formate is the best way to do that. It's will only add leading zero for a single length. 9 to "09" and 25 to "25".
String.format("%02d", value)
Bonus:
If you want to add multiple leading zero 9 to "0009" and 1000 to "1000". That's means you want a string for 4 indexes so the condition will be %04d.
String.format("%04d", value)
I don't know the exact syntax. But in any language, it would look like this.
a = 9
aString =""
if a < 10 then
aString="0" + a
else
aString = "" + a
end if

How to write number with sign on the left and thousands separator point

I am holding the number in character format in abap. Because I have to take the minus from right to left. So I have to put the number to character and shift or using function 'CLOI_PUT_SIGN_IN_FRONT' I'm moving minus character to left.
But after assigning number to character it doesn't hold the points. I mean my number is;
1.432- (as integer)
-1432 (as character)
I want;
-1.432 (as character)
is there a shortcut for this or should I append some string operations.
Edit:
Here is what I'm doing now.
data: mustbak_t(10) TYPE c,
mustbak like zsomething-menge.
select single menge from zsomething into mustbak where something eq something.
mustbak_t = mustbak.
CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
CHANGING
VALUE = mustbak_t.
write: mustbak_t.
If you're on a recent release, you could use string templates - you'll have to add some black magic to use a country that confoirms to your decimal settings, though:
DATA: l_country TYPE t005x-land,
l_text TYPE c LENGTH 15,
l_num TYPE p LENGTH 6.
SELECT SINGLE land
INTO l_country
FROM t005x
WHERE xdezp = space.
l_num = '-123456'.
l_text = |{ l_num COUNTRY = l_country }|.
WRITE: / l_text.
In this case, you need a country code to pass to the COUNTRY parameter as described in the format options. The values of the individual fields, namely T005X-XDEZP are described in detail in the country-specific formats.
tl;dr = Find any country where they use "." as a thousands separator and "," as a decimal separator and use that country settings to format the number.
You could also use classic formatting templates, but they are hard to handle unless you have a fixed-length output value:
DATA: l_text TYPE c LENGTH 15,
l_num TYPE p LENGTH 6 DECIMALS 2.
l_num = '-1234.56'.
WRITE l_num TO l_text USING EDIT MASK 'RRV________.__'.
CONDENSE l_text NO-GAPS.
WRITE: / l_text.
Here's another way, which i finally got working:
DATA: characters(18) TYPE c,
ints TYPE i VALUE -222333444.
WRITE ints TO characters. "This is it... nothing more to say.
CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
CHANGING
value = characters.
WRITE characters.
Since integers are automatically printed with the thousands separator, you can simply output them to a char data object directly using WRITE TO with no aditions..... lol
DATA: currency TYPE cdcurr,
characters(18) TYPE c,
ints TYPE i VALUE -200000.
currency = ints.
WRITE currency TO characters CURRENCY 'USD' DECIMALS 0.
CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
CHANGING
value = characters.
.
WRITE: / 'example',characters.
This prints your integer as specified. Must be apparently converted to a currency during the process.

what is the differences between +n and (n) in bit operations?

I've found two parameters defined like these:
&TM_PERIOD+4&/&TM_PERIOD(4)&
It's to pass data from a database to a form.
If the format of the data would be DDMMYYYY what are differences between those two parameters?
if TM_PRIOD is in form of DDMMYYYY then
TM_PERIOD(4) equals DDMM
TM_PERIOD+4 equals YYYY
the (4) means 4 characters
the +4 means after the 4th character
TM_PERIOD+1(2) = DM
(2 characters after the first)
These are not bit operations. +n specifies a string offset and (n) specifies the length.
They can be used independently of each other as well, so you can use just +n or just (n).
So:
data: lv_text(20) type c.
lv_text = "Hello".
write: / lv_text+2(3).
would output 'llo', for example.