Set standard number format with thousand separator, 2 decimals and minus sign for negative numbers using VBA? - vba

I've seen the question asked before on stackoverflow, how to get normal number format with thousand separator and 2 decimals. The answer was to set:
rng.NumberFormat = "##0.00"
But this is incomplete, because, at least on my computer, I don't get any space separator between millions and thousands. So I have changed that to:
rng.NumberFormat = "### ### ##0.00"
But this is again incomplete, because for some reason negative numbers were formatted to look like they have a space between the minus sign and the number. See below:
- 12.4
So, there are some things left to do to arrive at Excels "built-in" "format as number" formats. Additionally the formatting that I apply though VBA is described as Custom by Excel.
Is there any way to set the format to be the standard built in format as number with thousand separators, 2 decimals and minus signs for negative numbers?
I'm looking for something like:
rng.NumberFormat = "Number, 2, minus"

rng.NumberFormat = "# ##0.00:-# ##0.00"
You put the format for positive numbers before : and the format for negative after. You don't need to put hundreds of # signs in the format, just enough to show what the 1000's separator is.

Related

Remove decimal separator with output format on real values in Fortran [duplicate]

So I have some code that does essentially this:
REAL, DIMENSION(31) :: month_data
INTEGER :: no_days
no_days = get_no_days()
month_data = [fill array with some values]
WRITE(1000,*) (month_data(d), d=1,no_days)
So I have an array with values for each month, in a loop I fill the array with a certain number of values based on how many days there are in that month, then write out the results into a file.
It took me quite some time to wrap my head around the whole 'write out an array in one go' aspect of WRITE, but this seems to work.
However this way, it writes out the numbers in the array like this (example for January, so 31 values):
0.00000 10.0000 20.0000 30.0000 40.0000 50.0000 60.0000
70.0000 80.0000 90.0000 100.000 110.000 120.000 130.000
140.000 150.000 160.000 170.000 180.000 190.000 200.000
210.000 220.000 230.000 240.000 250.000 260.000 270.000
280.000 290.000 300.000
So it prefixes a lot of spaces (presumably to make columns line up even when there are larger values in the array), and it wraps lines to make it not exceed a certain width (I think 128 chars? not sure).
I don't really mind the extra spaces (although they inflate my file sizes considerably, so it would be nice to fix that too...) but the breaking-up-lines screws up my other tooling. I've tried reading several Fortran manuals, but while some of the mention 'output formatting', I have yet to find one that mentions newlines or columns.
So, how do I control how arrays are written out when using the syntax above in Fortran?
(also, while we're at it, how do I control the nr of decimal digits? I know these are all integer values so I'd like to leave out any decimals all together, but I can't change the data type to INTEGER in my code because of reasons).
You probably want something similar to
WRITE(1000,'(31(F6.0,1X))') (month_data(d), d=1,no_days)
Explanation:
The use of * as the format specification is called list directed I/O: it is easy to code, but you are giving away all control over the format to the processor. In order to control the format you need to provide explicit formatting, via a label to a FORMAT statement or via a character variable.
Use the F edit descriptor for real variables in decimal form. Their syntax is Fw.d, where w is the width of the field and d is the number of decimal places, including the decimal sign. F6.0 therefore means a field of 6 characters of width with no decimal places.
Spaces can be added with the X control edit descriptor.
Repetitions of edit descriptors can be indicated with the number of repetitions before a symbol.
Groups can be created with (...), and they can be repeated if preceded by a number of repetitions.
No more items are printed beyond the last provided variable, even if the format specifies how to print more items than the ones actually provided - so you can ask for 31 repetitions even if for some months you will only print data for 30 or 28 days.
Besides,
New lines could be added with the / control edit descriptor; e.g., if you wanted to print the data with 10 values per row, you could do
WRITE(1000,'(4(10(F6.0,:,1X),/))') (month_data(d), d=1,no_days)
Note the : control edit descriptor in this second example: it indicates that, if there are no more items to print, nothing else should be printed - not even spaces corresponding to control edit descriptors such as X or /. While it could have been used in the previous example, it is more relevant here, in order to ensure that, if no_days is a multiple of 10, there isn't an empty line after the 3 rows of data.
If you want to completely remove the decimal symbol, you would need to rather print the nearest integers using the nint intrinsic and the Iw (integer) descriptor:
WRITE(1000,'(31(I6,1X))') (nint(month_data(d)), d=1,no_days)

Preceding Zeros on Numbers (With Letters on Some)

an awful post title I know, struggled with that one!
Basically I have a list of numbers ranging from 1 - 230(ish)
I'm using sprintf( '%03d', $num) to output the numbers so that they have preceding zeros if necessary.
However something has buggered it all up, as I see some of the numbers also have letters in them (e.g. 134a, 134b etc).
So those numbers all currently output as '134'
I tried setting sprintf to %04d but that didn't work either, it just displayed them as '0134'
Does anyone have any suggestions?
Sounds like hexadecimal numbers. Use "%04x".

How can you format the result of a formula as s number

I'm using a formula in Excel 2007 to grab a mailbox size from a string. I'm stripping out all of the text before and after, and removing the , characters, yet Excel will not format the result as a number.
Because of this, I can't run any statistics such as total size or average size.
=SUBSTITUTE(MID(MailboxEX01[[#This Row],[TotalItemSize]], FIND("(", MailboxEX01[[#This Row],[TotalItemSize]]) + 1, FIND("bytes", MailboxEX01[[#This Row],[TotalItemSize]]) - (FIND("(", MailboxEX01[[#This Row],[TotalItemSize]]) + 2)), ",", "")
I tried =TEXT({the above}, "#,##0"), which successfully added the , character as the thousands separator, but (I guess unsurprisingly) still failed to format the cell as a number.
Does anybody know how I can force the result in this cell to format as a number? Thanks.
Your formula takes text values, strips stuff out, and replaces stuff. But the result is still text. If the result of that formula contains only the characters 0 to 9, you can coerce it into a number by using
={your formula}+0
or
={your formula}*1
or
=--({your formula})
Using =TEXT({your formula}, "format") will not produce a number. The TEXT() function returns text, as the name implies.
But if you use a mathematical operator on a number that is stored as text (or a text that represents a number), the maths operation will coerce that text value into a real number. The third suggestion uses the double unary (Google that) to coerce the text into a number.

Correct termiology for documentation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
The documentation below is for a module, which has now been "decommissioned"
and I'm writing it's replacement.
Before i write the replacement I want to get my terms right.
I know the terms are wrong in the documentation - it was hacked together quickly
so i could instruct a college working on the hardware side of this project on how to use a program I'ld made.
Full documentary can be found here for any who are interested (in so much as has been written and added to our wiki), the Website may only be available to certain IPS's (depends on you ISP - university internet connections are most likely to work), and the SVN repo is private.
So there are alot of terms that are wrong.
such as.
deliminators
formatted string containing value expressions (might now be wrong but is hard to say)
What are the correct terms for these.
And what other mistakes have I made
==== formatted string containing value expressions ====
Before I start on actual programs an explanation of:
"formatted string containing value expressions" and how to encode values in them.
The ''formatted string containing value expressions'' is at the core of doing low level transmission.
We know the decimal 65, hex 41, binary 0100 0001, and the ascii character 'A' all have the same binary representation, so to tell which we are using we have a series of deliminators - numbers preceded by:
# are decimal
$ are Hex
# are binary
No deliminator, then ascii.
Putting a sign indicator after the deliminator is optional. It is required if you want to send a negative number.
You may put muliple values in the same string.
eg: "a#21#1001111$-0F"
All values in a ''formatted string containing value expressions'' must be in the range -128 to 255 (inclusive) as they must fit in 8bytes (other values will cause an error). Negative numbers have the compliment of 2 representation for their binary form.
There are some problems with ascii - characters that can't be sent (in future versions this will be fixed by giving ascii a delineator and some more code to make that deliminator work, I think).
Characters that can't be sent:
* The delineator characters: $##
* Numbers written immediately after a value that could have contained those digits:
* 0,1,2,3,4,5,6,7,8,9 for decimal
* 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,A,B,C,D,E,F for hex
* 0,1 for binary
For a start, deliminator would probably be delimiter, although I notice your text has both delineator and deliminator in it - perhaps deliminator is a special delimiter/terminator combination :-)
However, a delimiter is usually used to separate fields and is usually present no matter what. What you have is an optional prefix which dictates the following field type. So I would probably call that a "prefix" or "type prefix" instead.
The "formatted string containing value expressions" I would just call a "value expression string" or "value string" to change it to a shorter form.
One other possible problem:
must be in the range -128 to 255 (inclusive) as they must fit in 8bytes
I think you mean 8 bits.
Try something like the following:
==== Value string encoding ====
The value string is at the core of the data used for low level
transmissions.
Within the value string the following refixes are used:
# decimal
$ Hex
# binary
No prefix - ASCII.
An optional sign may be included after the delimiter for negative numbers.
Negative numbers are represented using twos complement.
The value string may contain multiple values:
eg: "a#21#1001111$-0F"
All elements of the value string must represent an 8bit value and must
be in the range -128 to 255
When using ASCII representation the following characters that can't be sent
* The delineator characters: $## (use prefixed hex value.)
* Numbers written immediately after a value that could have
contained those digits:
* 0,1,2,3,4,5,6,7,8,9 for decimal
* 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,A,B,C,D,E,F for hex
* 0,1 for binary

unwanted leading blank space on oracle number format

I need to pad numbers with leading zeros (total 8 digits) for display. I'm using oracle.
select to_char(1011,'00000000') OPE_NO from dual;
select length(to_char(1011,'00000000')) OPE_NO from dual;
Instead of '00001011' I get ' 00001011'.
Why do I get an extra leading blank space? What is the correct number formatting string to accomplish this?
P.S. I realise I can just use trim(), but I want to understand number formatting better.
#Eddie: I already read the documentation. And yet I still don't understand how to get rid of the leading whitespace.
#David: So does that mean there's no way but to use trim()?
Use FM (Fill Mode), e.g.
select to_char(1011,'FM00000000') OPE_NO from dual;
From that same documentation mentioned by EddieAwad:
Negative return values automatically
contain a leading negative sign and
positive values automatically contain
a leading space unless the format
model contains the MI, S, or PR format
element.
EDIT: The right way is to use the FM modifier, as answered by Steve Bosman. Read the section about Format Model Modifiers for more info.