What does the format and width stand for in DLGCreateRealField - dialog

What does the format and the width stand for in the DLGCreateRealField(number value, number width, number format) dialog function? The documentation does not say anything about it.

TL;DR / Most cases
The width is the display width of the input field in the dialog.
The format tells how many digits in total to show keeping at least one digit after the decimal separator. The input is only formatted in the initial state and on the return value but only if the input is changed (with some more exceptions, see below). The user can type as many digits as she/he wants. The value is rounded. Example: For format=3 the value 1.234 is shown as 1.23; the value 123.456 is shown as 123.4.
Note that there are some weird corner cases mentioned below.
Unexpected behaviour summary
Return value is only formatted/rounded correctly when the input is touched (otherwise 6 digits)
Return value is not rounded if the input has exactly 6 digits before the decimal separator (no matter which format)
The shown values are converted to exponent notation (e.g. 1.2e8) if the value is gerater than 100 000 000 (no matter which format)
Details
The image shows an example dialog created with the code below. The first three inputs have the width of 8, the last three inputs have the width of 16. This is easy to understand.
Now the format: Each input has the format equal to its label. The first inputs format is 2, the last inputs format is 9. On creation the input shows as many digits as specified but at least one digit after the decimal separator. For the first input this is two digits in total. But because 12 already contains two digits, the first digit after the decimal separator is kept. All the other inputs show this behaviour as there are never more digits in total than specified. As you can see on Seven the number is rounded, not just cut off.
The user can type in as many digits as she/he wants. (Only digits, e, minus and the dot are allowed. All characters after a second dot are ignored.)
If the user does not change anything and presses OK, the returned value is rounded on 6 digits with the rules explained above. As soon as one touches the input (also type in one digit and then remove it immediately is enough), the input will be rounded on the format... Except if the value in the input has exactly 6 digits before the decimal separator. Then it is returned as an integer, the initial value is still formatted correctly. Phew...
Note that on some point the numbers are converted to the exponential notation. Also in the input field! This means the value 123456789 is shown as 1.23456789e+8. The exponent notation follows the format rules again (not counting the exponent digits). This means in the first input field with format=2 the value is shown as 1.2e+8.
The following code was used to create the screenshot.
TagGroup DLG, DLGItems;
DLG = DLGCreateDialog( "Please enter values", DLGItems);
TagGroup val2tg, val3tg, val4tg, val5tg, val7tg, val9tg;
DLGitems.DLGAddElement(DLGCreateRealField("Two: ", val2tg, 12.123456789123, 8, 2));
DLGitems.DLGAddElement(DLGCreateRealField("Three:", val3tg, 12.123456789123, 8, 3));
DLGitems.DLGAddElement(DLGCreateRealField("Four: ", val4tg, 12.123456789123, 8, 4));
DLGitems.DLGAddElement(DLGCreateRealField("Five: ", val5tg, 12.123456789123, 16, 5));
DLGitems.DLGAddElement(DLGCreateRealField("Seven:", val7tg, 12.123456789123, 16, 7));
DLGitems.DLGAddElement(DLGCreateRealField("Nine: ", val9tg, 12.123456789123, 16, 9));
object dialog = alloc(UIFrame).init(DLG);
if(dialog.pose()){
Result("\n= = =\n");
Result("Two: " + val2tg.DLGGetValue() + "\n");
Result("Three: " + val3tg.DLGGetValue() + "\n");
Result("Four: " + val4tg.DLGGetValue() + "\n");
Result("Five: " + val5tg.DLGGetValue() + "\n");
Result("Seven: " + val7tg.DLGGetValue() + "\n");
Result("Nine: " + val9tg.DLGGetValue() + "\n");
}

Related

Legal Statute Sorting Algorithm (Algorithmic Challenge)

I have designed a down and dirty sorting algorithm for New Jersey Legal Statutes, but I'm looking for a better way. Statutes are formatted in the following manner:
Title - A number < 999 and may include up to 2 letters after the title. Ex. 26, 26A, 26AA
Chapter - Formatted exactly the same as title.
Paragraph - A number < 999 which may be followed by 1-2 letters, and or a decimal point, and or a number < 999, and or a letter, and or another number. Ex. 25, 25.26, 25a, 25a.26, 25a.26b, 25aa.26, etc.
I convert the title and paragraph to decimals by stripping out the whole number in the beginning and dividing by 1000, giving me a decimal value which is converted to a string. I have assigned all letters(converted to lowercase) string values from 01-26. I add those values to the end of the initial whole number string sequentially. Any numbers also mixed in are added as their string value.
The obvious bottleneck is the mess of possibilities in the paragraph section. I have actually split that up to paragraph (pre any decimal) and section. I apply the above logic to the broken down sections if they exist.
As for the sorting 17 < 17A < 17AA < 17B < 18.
An example value conversion of 17B:26bb-2a5.1a5 would break down as the following:
Title- .01702
Chapter- .0260202
Paragraph- .002015
Section- .001015
Some more examples of statutes:
17:2-3
18B:2a-1
19AA:3-56g
26:56a-16
1:56-12.123
2:34–15.12a
The method I've devised is pretty dirty. I had to split it up in sections to ensure I had the correct values for each 'section' converting the whole number part to a decimal. I'm also using JS(Node) which doesn't handle large numbers well.
If anyone has a more efficient/clean way, any thoughts, or feedback, I'd greatly appreciate it.

Keeping only letters and digits in a string

I am recoding some open survey responses in SPSS and am wanting just to keep the usual characters a-z and 1-9
I have done rtrim and ltrim which has worked on the majority, but some strings have trailing spaces remaining, which I am assuming are not actually spaces but are hidden characters.
I have also removed punctuation such as "?" but I imagine there must be a more straightforward way than going through each one.
e.g. I need
"exam'ple! " or " exam!!--ple?"
to say "example"
The following syntax will create a new clean field and copy to it only the digits and letters (uppercase or lowercase) from the original field.
Note that I used 15 as the new field width and as the number of iterations in the loop - please change 15 to the actual width of the original field
do repeat val=1 to 15.
compute #i = number(char.substr(OrigField, val, 1), PIB).
if range(#i, 48, 57) or
range(#i, 65, 90) or
range(#i, 97, 122)
CleanField=concat(rtrim(CleanField), char.substr(OrigField, val, 1)).
end repeat.
exe.
See the link suggested by #user45392 to understand how/why this works.
Also see this list for additional values you can add to the loop if you'd like.

Hyphen with strings in PROC FORMAT

I am working with IC9 codes and am creating somewhat of a mapping between codes and an integer:
proc format library = &formatlib;
invalue category other = 0
'410'-'410.99', '425.4'-'425.99' = 1
I have searched and searched, but haven't been able to find an explanation of how that range actually works when it comes to formatting.
Take the first range, for example. I assume SAS interprets '410'-'410.99' as "take every value between the inclusive range [410, 410.99] and convert it to a 1. Please correct me if I'm wrong in that assumption. Does SAS treat these seeming strings as floating-point decimals, then? I think that must be the case if these are to be numerical ranges for formatting all codes within the range.
I'm coming to SAS from the worlds of R and Python, and thus the way quote characters are used in SAS sometimes is unclear (like when using %let foo = bar... not quotes are used).
When SAS compares string values with normal comparison operators, what it does is compare the byte representation of each character in the string, one at a time, until it reaches a difference.
So what you're going to see here is when a string is input, it will be compared to the 'start' string and, if greater than start, then compared to the 'end' string, and if less than end, evaluated to a 1; if it's not for each pair listed, then evaluated to a zero.
Importantly, this means that some nonsensical results could occur - see the last row of the following test, for example.
proc format;
invalue category other = 0
'410'-'410.99', '425.4'-'425.99' = 1
;
quit;
data test;
input #1 testval $6.;
category=input(testval,category.);
datalines;
425.23
425.45
425.40
410#
410.00
410.AA
410.7A
;;;;
run;
410.7A is compared to 410 and found greater, as '4'='4', '1'='1', '0'='0', '.' > ' ', so greater . Then 410.7A is compared to 410.99 and found less, as '4'='4', '1'='1', '0'='0', '7' < '9', so less. The A is irrelevant to the comparison. But on the row above it you see it's not in the sequence, since A is ASCII 41x and that is not less than '9' (ASCII 39x).
Note that all SAS strings are filled to their full length by spaces. This can be important in string comparisons, because space is the lowest-valued printable character (if you consider space printable). Thus any character you're likely to compare to space will be higher - so for example the fourth row (410#) is a 1 because # is between and . in the ASCII table! But change that to / and it fails. Similarly, change it to byte(13) (through code) and it fails - because it is then less than space (so 410^M, with ^M representing byte(13), is less than start (410)). In informats and formats, SAS will treat the format/informat start/end as being whatever the length that it needs to - so if you're reading a 6 long string, it will treat it as length 6 and fill the rest with spaces.

Silverlight 4 StringFormat for double without decimals in XAML [duplicate]

I have a double value. I want to format this value in the format of x.yz. How do I do this? I keep getting digits truncated. Can someone tell me how to do this in C#?
Thanks!
Digits after decimal point
This example formats double to string with fixed number of decimal places. For two decimal places use pattern „0.00“. If a float number has less decimal places, the rest digits on the right will be zeroes. If it has more decimal places, the number will be rounded.
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"
Next example formats double to string with floating number of decimal places. E.g. for maximal two decimal places use pattern „0.##“.
// max. two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"
Digits before decimal point
If you want a float number to have any minimal number of digits before decimal point use N-times zero before decimal point. E.g. pattern „00.0“ formats a float number to string with at least two digits before decimal point and one digit after that.
// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567); // "123.5"
String.Format("{0:00.0}", 23.4567); // "23.5"
String.Format("{0:00.0}", 3.4567); // "03.5"
String.Format("{0:00.0}", -3.4567); // "-03.5"
Thousands separator
To format double to string with use of thousands separator use zero and comma separator before an usual float formatting pattern, e.g. pattern „0,0.0“ formats the number to use thousands separators and to have one decimal place.
String.Format("{0:0,0.0}", 12345.67); // "12,345.7"
String.Format("{0:0,0}", 12345.67); // "12,346"
Zero
Float numbers between zero and one can be formatted in two ways, with or without leading zero before decimal point. To format number without a leading zero use # before point. For example „#.0“ formats number to have one decimal place and zero to N digits before decimal point (e.g. „.5“ or „123.5“).
Following code shows how can be formatted a zero (of double type).
String.Format("{0:0.0}", 0.0); // "0.0"
String.Format("{0:0.#}", 0.0); // "0"
String.Format("{0:#.0}", 0.0); // ".0"
String.Format("{0:#.#}", 0.0); // ""
Align numbers with spaces
To align float number to the right use comma „,“ option before the colon. Type comma followed by a number of spaces, e.g. „0,10:0.0“ (this can be used only in String.Format method, not in double.ToString method). To align numbers to the left use negative number of spaces.
String.Format("{0,10:0.0}", 123.4567); // " 123.5"
String.Format("{0,-10:0.0}", 123.4567); // "123.5 "
String.Format("{0,10:0.0}", -123.4567); // " -123.5"
String.Format("{0,-10:0.0}", -123.4567); // "-123.5 "
Custom formatting for negative numbers and zero
If you need to use custom format for negative float numbers or zero, use semicolon separator „;“ to split pattern to three sections. The first section formats positive numbers, the second section formats negative numbers and the third section formats zero. If you omit the last section, zero will be formatted using the first section.
String.Format("{0:0.00;minus 0.00;zero}", 123.4567); // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567); // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0); // "zero"
http://www.csharp-examples.net/string-format-double/
Using format strings is explained in:
Standard Numeric Format Strings
Custom Numeric Format Strings
For example, try:
(0.56789).ToString("F2")
(0.56789).ToString("0.00").
Note that the resulting value is NOT truncated, but rounded in both cases, resulting in "0.57".
string.Format("{0:0.00}",yourdouble);
And maybe you'll find useful stick a paper with this http://john-sheehan.com/blog/wp-content/uploads/msnet-formatting-strings.pdf somewhere in your office
Tried something like this, using ToString?
doubleNumber = -1898300.1937;
Console.WriteLine(doubleNumber.ToString("F2", CultureInfo.InvariantCulture));
// Displays -1898300.19
I'm using Math.Round Method
Math.Round(yourdouble, 2)
You can also specify the rounding rule.
Try this:
number.ToString("0.00");
Also take a look at Custom Numeric Format Strings

How do I convert string of hex digits to value it represents in Lua

I'm reading in a lot of lines of hex data. They come in as strings and I parse them for line_codes which tell me what to do with the rest of the data. One line sets a most significant word of an address (MSW), another line sets the least significant (LSW).
I then need to concatenate those together such that if MSW = "00ff" and LSW = "f10a"
address would be 00fff10a.
This all went fine, but then I was supposed to check if address was between a certain set of values:
if address <= "007FFFh" and address >= "000200h" then
print "I'm in"
end
As you all probably know, Lua is not a fan of this as it gives me an error using <= and >= with strings.
If there a way I can convert the string into hex, such that "FFFF" would become 0xFFFF?
You use tonumber:
local someHexString = "03FFACB"
local someNumber = tonumber(someHexString, 16)
Note that numbers are not in hexadecimal. Nor are they in decimal, octal, or anything else. They're just numbers. The number 0xFF is the same number as 255. "FF" and "255" are string representations of the same number.

Resources