How to easily calculate the sum of digits?
How to easily calculate the sum of digits?
How to easily calculate the sum of digits in any text without having to add "+" and "=" all the time and paste it into the calculator.
Numbers in lines or after a comma.
Sample 1:
1
56
11
9
37
78
80
Sample 2:
05,07,08,12,14,19,21,29,30,35,43,49,50,61,64,68,70,73,74,80
Numbers in lines or after a comma.
Sample 1:
1
56
11
9
37
78
80
Sample 2:
05,07,08,12,14,19,21,29,30,35,43,49,50,61,64,68,70,73,74,80
Re: How to easily calculate the sum of digits?
addup.bat:
For the comma, you could do something like:
Or you could have your gawk do a running total...
Gawk for Windows
Code: Select all
gawk "{ sum += $1 } END { print sum }" %1 %2 %3
@echo off
pause
:END
ECHO DONE
Code: Select all
C:\BIN>addup
C:\BIN>gawk "{ sum += $1 } END { print sum }"
1
2
3
^Z
6
Press any key to continue . . .
DONE
C:\BIN>
Code: Select all
sed -e s/,/\n/g < setofcommanumbers | addup
Code: Select all
C:\TMP\>sed -e s/,/\n/g < setofcommanumbers | addup
C:\TMP\>gawk "{ sum += $1 } END { print sum }"
812
Press any key to continue . . .
DONE
C:\TMP\>
Gawk for Windows
Re: How to easily calculate the sum of digits?
This was my original script, running under Xenix.
(It worked MUCH better then what I was able to accomplish under DOS.)
Not only could you run this from the command-line, but you could also call it from within a text editor (like Vim).
(You can do similar with what's in the 1st. post, but it it nowhere near as clean, as nice.)
(It worked MUCH better then what I was able to accomplish under DOS.)
Code: Select all
#
# @(#) addup.sh 1.0 90/07/19
#
# Copyright (C) therube, 1990
# Adds up a column (default=last) of numbers in a file.
# 95/05/16 updated to allow (999) negative style numbers.
case $1 in
-[0-9])
COLUMN=`echo $1 | tr -d -`
shift
;;
*)
COLUMN="NF"
;;
esac
echo "Adding up column .. $COLUMN .. of file(s) .. $*"
nawk ' OFMT="%.2f" # 1 "%12.2f"
{ x = '$COLUMN' # 2
neg = index($x, "$") # 3
if (neg > 0) X = gsub("\\$", "", $x)
neg = index($x, ",") # 4
if (neg > 1) X = gsub(",", "", $x)
neg = index($x, "(") # 8 neg (123 & change
if (neg > 0) X = gsub("\\(", "", $x)
if (neg > 0) $x = (-1 * $x) # it to "-123.00"
neg = index($x, "-") # 5
if (neg > 1) $x = (-1 * $x) # 6
t += $x # 7
print "x is <<<", $x+0, ">>> running balance:", t
} ' $*
# 1. set numeric format to eliminate rounding errors
# 1.1 had to reset numeric format from 12.2f to .2f 95/05/16
# when a computed number is assigned to a variable ( $x = (-1 * $x) )
# it causes $x to use the OFMT so -1.23 = "________-1.23" vs "-1.23"
# and that causes my #5 (negative check) to not work correctly because
# the index returns a number >1 and to the neg neg than becomes a positive
# this only occurs if the number happened to b a "(" neg number
# 2. find the field we want to add up (comes from the shell or defaults
# to the last field "NF") in the file
# 3. check for a dollar sign ($) in the number - if there get rid of it
# so we may add it correctly - $12 $1$2 $1$2$ $$1$$2$$ all = 12
# 4. check for a comma (,) in the number - if there get rid of it so we
# may add it correctly - 1,2 12, 1,,2 1,,2,, all = 12 (,12=0)
# 5. check for negative numbers
# 6. if x is a negative number in the form 999- "make" it a recognized
# number like -999 - if x is a negative number like -999 already
# the test fails (y is not >1) and this "true" negative is not made
# positive
# 7. accumulate the total
# 8. if x is a negative number in the form (999) "make it a recognized
# number like -999
# * Note that a (-9) (neg neg number) returns a postive
# * Mite not work rite with all forms of all numbers using $-,+. etc. *
(You can do similar with what's in the 1st. post, but it it nowhere near as clean, as nice.)
Re: How to easily calculate the sum of digits?
Eval calculator (command line version)
With that, you would have to add + to the numbers, on a single line.
abunchofnumbers:
With that, you would have to add + to the numbers, on a single line.
Code: Select all
C:\TMP\>eval < abunchofnumbers
>> +05 +07 +08 +12 +14 +19 +21 +29 +30 +35 +43 +49 +50 +61 +64 +68 +70 +73 +74 +80
ans[0]= 812
C:\TMP\>
Code: Select all
+05 +07 +08 +12 +14 +19 +21 +29 +30 +35 +43 +49 +50 +61 +64 +68 +70 +73 +74 +80
Re: How to easily calculate the sum of digits?
,
Last edited by Debugger on Fri Jan 11, 2019 5:19 pm, edited 1 time in total.
Re: How to easily calculate the sum of digits?
Here we ago again with the "attitude". Don't you think its rude to reply like this to someone who is trying to help you in gaining knowledge.but useless for me
Please don't try to take the easy way out for all the problems.
Each and everyone's issue/problem/requirement is different than others. So there won't be any ready-made solution for all. I'm dying to say this for quite a looong time.
Further, you should realise now that many of the forum members are avoiding your threads, only because of the attitude.
[I apologise for this outburst, but someone has to say it]
[Note: If any moderators/admin feel this post should be deleted, pls feel free to delete this]
Re: How to easily calculate the sum of digits?
I'm not a very good programmer, but i tried.
https://www120.zippyshare.com/v/IeiQ6iHo/file.html
[Edit: As there were some issues with previous one, updated the image and links]
Last edited by vanisk on Sun Jan 13, 2019 8:11 pm, edited 7 times in total.
Re: How to easily calculate the sum of digits?
vanisk - Perfectly, that's what I wanted, for a simple and quick calculation. Many thanks!
Re: How to easily calculate the sum of digits?
That sounds extremely useful! If you ever find such a tool, please let me know. That would make everything so much easier!
BTW: I am one of those persons who stopped reading @Debugger's posts - a long time ago- as he/she is too unfriendly to my liking to people who are trying to help him by spending their time, skills and knowledge.
At least, that's how it was a couple of months ago.
Please let me know if that changes (for the good, of course )
Re: How to easily calculate the sum of digits?
It's impossible, no chance. The same things at https://ghisler.ch/board (makinero vs Total Commander)
Re: How to easily calculate the sum of digits?
ovg - Debugger and Makinero are two completely different people, but at the request of the user "Makinero" I publish topics on various forums in order to solve the issue or problem as soon as possible.
Это смешно, забавно, весело
NotNull - what is the end for me enough of the calculated requirements
I will give you a dot with an exclamation mark ...now I am lying among the infinite
vanisk - I try not to make life difficult for others. I do not offend anyone, I do not provoke anything, I do not assume controversial threads. Was the appointment of a troll to me as a result of ordinary antipathy to me?
Это смешно, забавно, весело
NotNull - what is the end for me enough of the calculated requirements
I will give you a dot with an exclamation mark ...now I am lying among the infinite
vanisk - I try not to make life difficult for others. I do not offend anyone, I do not provoke anything, I do not assume controversial threads. Was the appointment of a troll to me as a result of ordinary antipathy to me?
Re: How to easily calculate the sum of digits?
I ALWAYS try to make life of others a little bit easier.
I didn't quite understand the statement. However, what we are trying to say is just give others some respect (for the time they take to answer you and for trying to help).I do not offend anyone, I do not provoke anything, I do not assume controversial threads. Was the appointment of a troll to me as a result of ordinary antipathy to me?
Finally, i have no intention of offending/hurting anyone. If my comment did offend anyone, i apologize.
"To Err is Human"
Re: How to easily calculate the sum of digits?
@SuperDude - Finally! I found an amateur program in my collections. Written once by a friend, especially for me. It's called "Sumator." For example, with 20 numbers, it removes duplicates and shows numbers from 1 to 80.